Search code examples
iosswiftxcodeios9swift4

How to add button that pass value to textbox in xcode


I'm new to xcode. I am creating converter app using decimalpad keyboard. I need to add button that have value (-). When ii click this button, this value should appear in my textbox. How to do it.


Solution

  • I suggest a tutorial on starting iOS app development, such as those provided by AppCoda or Hacking With Swift.

    You will need to add at least a UIButton and a UILabel to your app's main viewController. You will then need to ctrl+drag these to your viewController code to create an IBOutlet, so the button and label can be accessed by the code.

    Then use code like this:

    // The Interface Builder Outlets
     @IBOutlet weak var myTextField: UITextField!
     @IBOutlet weak var myLabel: UILabel!
    
    // The Interface Builder Action for the button
    @IBAction func buttonTapped(_ sender: UIButton) {
        myLabel.text = "Your text here!"
    }
    

    Alternatively, I could be misunderstanding your needs. If you want a custom button on the keyboard to input a "-" symbol, you will need to create a custom keyboard extension and set it as the inputView of the UITextField. You would then need code to parse the text of your UITextField to get the result you need.

    There is a tutorial on this here.