Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the text of a button when it is clicked? - Swift

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.)

like image 739
Jaxon Avatar asked Feb 12 '15 03:02

Jaxon


People also ask

How do I change the text of a UIButton in Swift?

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:

How do I change the text of a button in 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: @IBOutlet weak var button: UIButton!

How to change button text in flutter?

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.

How to change the text of a UIButton programmatically?

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”.


2 Answers

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:

enter image description here

like image 161
Aaron Brager Avatar answered Oct 06 '22 20:10

Aaron Brager


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)
}
like image 27
Jeremy Pope Avatar answered Oct 06 '22 20:10

Jeremy Pope