Taylor T
Taylor T

Reputation: 11

How to make 2 buttons change a label value in Xcode

I would like to know if there is a way to make 2 separate buttons change the value of 1 label. Example: Button 1 makes the label say "Hello"; while, the second button makes the label say "Goodbye". I can easily make a label display a value just by pressing a button. I have even created a counter so that when the button is pressed agin the text disappears.

Upvotes: 1

Views: 3380

Answers (3)

Jim Bray
Jim Bray

Reputation: 788

For Xcode 10.2 with Swift 5

You can also use Swift to change a Mac OS label with a button by using the assistant editor. Ensure your storyboard is on the left and the class connected to the window view is displayed on the right (on a starter project the file name defaults to: ViewController.swift).

  1. Connect the label to the NSViewController (control + dragging into code) just under the class statement (Near the top)
    and when the prompt appears.

          Connection = Outlet
          Name = "label" or "your label name"
          Type = Any

    The resulting code will look like this
          @IBOutlet weak var label: NSTextField!

  2. Connect the button to the NSViewController (control + dragging into code) usually anywhere after any of the canned override statements close to the bottom but just above the last curly bracket
    when the prompt appears.

          Connection = Action
          Name = "Button" or "your button name"
          Type = Any

    The resulting code should appear like this
          @IBAction func button(_ sender: Any) {
          }

  3. Then add the following code between the two curly brackets created by the button connection.

    @IBAction func button(_ sender: Any) { self.label.stringValue = "New Label" }

  4. Run your project



LOL, I was originally having a senior moment and was searching how to change a button label when I ran across this entry and remembered how to do it.

If you are interested in changing a button label too, it's basically the same process

Connect the button to the code under the class statement and make a Outlet connection just like step 1. Then, at the point in the code where you want the button label to change, add the following line of code.

self.button.title = "New Button Label"

Hopes this helps anyone who needs it.

Upvotes: 1

esqew
esqew

Reputation: 44699

Code:

-(IBAction)hello:(id)sender {
    label1.text = @"Hello.";
}

-(IBAction)goodbye:(id)sender {
    label1.text = @"Goodbye.";
}

Link up your buttons to these IBActions respectively and the label to the outlet and you should be good to go.

Upvotes: 4

jrtc27
jrtc27

Reputation: 8526

Add the following to your view controller's .m:

-(IBAction)hello:(id)sender {
    label.text = @"Hello";
}

-(IBAction)goodbye:(id)sender {
    label.text = @"Goodbye";
}

and the following to the .h:

-(IBAction)hello:(id)sender;
-(IBAction)goodbye:(id)sender;

Then in Interface Builder link the methods to the buttons. Of course you need your label to be connected via Interface Builder and have a reference in your code, but I am assuming you have done that based on the content of your question.

Upvotes: 1

Related Questions