I am trying to make my own keypad in XCODE, with SWIFT and ViewController.
I have a label and few separate buttons with their action functions. If I press on one button I would like the label to change to corresponding number, but if I click the button again I want the label to display the number again, not just once but how many times the button is clicked..
For example:
Button: Clicked once
Label = 'Hello'
Button: clicked again
Label = 'HelloHello'
Here is my code:
@IBOutlet var textLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func noOne(_ sender: UIButton) {
textLabel.text = "1"
}
.... Any tips?
You can do that like
@IBAction func noOne(_ sender: UIButton) {
textLabel.text += "1"
}
Or even you can make your method more generic and handle all button clicks in a single method. Link all button actions to a single IBAction
like
@IBAction func onClick(_ sender: UIButton) {
textLabel.text += sender.titleLabel?.text ?? ""
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.