I'm new to programming so keep that in mind. I have looked at several other questions on this site, and tried the answers they gave, but those haven't worked for me. The code I am trying to use to change the button to is this:
@IBAction func AnswerButtonA(sender: UIButton)
{
[AnswerButtonA setTitle:@"Hello World" forState:UIControlStateNormal];
}
This is the most common method to do this I have found but it brings up several errors "Expected declaration" "Expected attribute name" "expected ',' separator, and a few others.
Another thing I tried that others suggested is:
@IBAction func AnswerButtonA(sender: UIButton)
{
AnswerButtonA.setTitle("Hello World")
}
This as well as a few other things I've tried all bring up the error "'(UIButton) ->()' does not have a member named 'setTitle'.
I realize this may be a stupid question but I can't figure it out. This is the first app I have tried to develop on my own and I have done fine until this problem. Any help would be appreciated, and if you could go into detail about what I'm doing wrong that would be amazing!
(I only want the button to change when it is pressed, not when the view is loaded.)
Swift Change UIButton text – Method 1 (IBOutlet) In the first method, we will use an IBOutlet to change the button text. First, create a UIButton somewhere on the storyboard. Second, create the IBOutlet the normal way. Call the IBOutlet “button”. And you should end up with something like this:
In the first method, we will use an IBOutlet to change the button text. First, create a UIButton somewhere on the storyboard. Second, create the IBOutlet the normal way. Call the IBOutlet “button”. And you should end up with something like this: @IBOutlet weak var button: UIButton!
We use ElevatedButton in this flutter example. We also use the stateful widget as simple state management is required to change the button text. A variable is declared in the stateful child widget and the variable is changed when the button is pressed using setState. The variable is assigned as Button Text.
Like in Visual Studio you can change text like that. I’ll show you two methods on how you can change your UIButton text programmatically. In the first method, we will use an IBOutlet to change the button text. First, create a UIButton somewhere on the storyboard. Second, create the IBOutlet the normal way. Call the IBOutlet “button”.
In your first question you're trying to use Objective-C code in a Swift file.
In your second example, you're not passing the control state.
In both instances, as Jeremy Pope points out, you're referring to AnswerButtonA
as the method, not the button.
You need to create an outlet to your button (or use sender
as in Jeremy's answer).
From there, here's what you're looking for:
answerButtonA.setTitle("Hello World", forState: .Normal)
The method signature will pop up as you're typing:
The button being pressed is being sent as an argument called sender. AnswerButtonA is the method which you created. Just a style note, usually the first letter is lowercase for methods. So again, AnswerButtonA is the method that is called when you click the button, sender is the actual button clicked.
IBAction func AnswerButtonA(sender: UIButton)
{
//sender is the button that was pressed
sender.setTitle("HelloWorld", forState: .Normal)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With