title: Assign 'action' dynamically in uibutton.addTarget
body: <p>please bear with me, as I'm new to swift -4 weeks old-.</p>
  
  <p>I've created the following 2 functions in fileA.swift</p>
  
  <pre><code>func custombttn(theSelector:Selector)-&gt; UIButton{
  
          let bttn = UIButton(frame: CGRect(x:20, y:400, width:200, height:30))
              bttn.setTitle("tap this button", for: UIControlState.normal)
                  bttn.backgroundColor = UIColor.black
                      bttn.setTitleColor(UIColor.magenta, for: UIControlState.normal)
      bttn.addTarget(bttn, action: theSelector, for: UIControlEvents.touchUpInside)
  
          return bttn
  
  }
  
  func customtxtfld() -&gt; UITextField{
  
      let txtField = UITextField(frame: CGRect(x:20, y:360, width:200, height:30))
  
      txtField.borderStyle = UITextBorderStyle.roundedRect
          txtField.backgroundColor = UIColor.magenta
              txtField.placeholder = "Do you like me now..?"
  
      return txtField
  
  }
  </code></pre>
  
  <p>The reason behind the <code>custombttn(theSelector:Selector)</code>, is that i want to pass the function dynamically to the button in my viewcontroller file.</p>
  
  <p>Now, moving the fileB.swift, I have the following code...</p>
  
  <pre><code>class TabOneViewController: UIViewController{
  
      let txt = customtxtfld()
      let bttn = custombttn(theSelector: #selector(updatetxt))
  
      override func loadView() {
  
          super.loadView()
  
              view.addSubview(txt)
                  view.addSubview(bttn)
  
      }
  
       func updatetxt(){
  
          txt.text = "hello, you!"
      }
  
  }
  </code></pre>
  
  <p>Here is where things get tricky, when I attempt to build, I don't get any error (not even a warning). However, when I run the app, and tap the <code>bttn</code> in <em>fileB.swift</em>, I get the following error during runtime:</p>
  
  <blockquote>
    <p>Terminating app due to uncaught exception
    'NSInvalidArgumentException', reason: '-[UIButton updatetxt]:
    unrecognized selector sent to instance 0x7f8453415670'</p>
  </blockquote>
  
  <p>If I have 2 or more functions in my fileB.swift that I wish to assign dynamically to the action part of the addTarget, <strong>is there any way I can pass the selector dynamically to a button..?</strong></p>
  
  <p>Appreciate your time and assistance. Please let me know if I need to explain something further.</p>