Reputation: 12335
I am trying to change the title of a button using setTitle
I am getting no errors and my IBAction
is working but nothing happens.
What am I missing here?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var hourTimerButton: UIButton!
@IBAction func hourTimerButton(_ sender: Any) {
hourTimerButton.setTitle("test", for: [])
print("button")
}
override func viewDidLoad() {
super.viewDidLoad()
hourTimerButton.setTitle("test", for: .normal)
hourTimerButton.setTitle("test", for: [])
}
}
Upvotes: 0
Views: 1556
Reputation: 989
Try it this way:
Title1 and Title2 are two different titles for your UIButton
:
Change @IBAction func hourTimerButton(_ sender: Any)
to
@IBAction func hourTimerButton(_ sender: AnyObject)
and then do this:
if (sender.currentTitle == "Title1"){
button.setTitle("Title2", for: .normal)
}
Hope it works!
Upvotes: 0
Reputation: 2505
Check your button(hourTimerButton) references are properly connected, I have debugged your code now, everything is working fine. Better you can change sender
type as UIButton
instead of Any
.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var hourTimerButton: UIButton!
@IBAction func hourTimerButton(_ sender: UIBUtton) {
sender.setTitle("test", for: .normal)
print("button")
}
override func viewDidLoad() {
super.viewDidLoad()
hourTimerButton.setTitle("test", for: .normal)
}
}
Thanks.
Upvotes: 0
Reputation: 1076
Try setting button like this for normal state.
hourTimerButton.setTitle("test", for: .normal)
Upvotes: 1