0

I have a huge crazy scene in my story board that has 36 different buttons, and each one means something different when clicked on. I really don't want to go about creating 36 different methods, so how could I reference a button title or button name in a method that is called when one of the 36 buttons is pushed.

This is probably a simple question, but I'm new to iOS and Objective C...

Thanks!

N_A
  • 19,799
  • 4
  • 52
  • 98
The Man
  • 1,462
  • 3
  • 23
  • 45

6 Answers6

6

You can create a single method, like so:

- (IBAction)buttonTapped:(id)sender{

  // The button that was tapped is called "sender"
  // This will log out the title of the button

  //NSLog(@"Button: %@", sender.titleLabel.text);

  //Edit: You need a cast in the above line of code:

  NSLog(@"Button: %@", ((UIButton *)sender).titleLabel.text);
}

Then, you can use Interface Builder to connect to all of the buttons. You can have some sort of if/else logic to test which button was tapped.

You can check the titleLabel property, or you can assign an IBOutlet to each button and check for that.

For example:

if([sender isEqual:buttonOutlet1]){
  //If this button is attached to buttonOutlet1
  //do something
}

Alternatively, you can simply use the label of each button, not worrying about outlets.

A third option would be to generate and lay out the buttons in code, and then access them as elements of an array of buttons.

A fourth option would be to add tags to the buttons and check for the button's tag in your function.

Moshe
  • 57,511
  • 78
  • 272
  • 425
  • That works, but then I would have to go about creating outlets for each button. – The Man Apr 06 '12 at 16:32
  • @JoeBurnett - Or you can use the titleLabel or use an array. – Moshe Apr 06 '12 at 16:35
  • When I do this... NSLog(@"Button: %@", sender.titleLabel.text); --- I get this error... Property 'titleLabel' not found on object of type '__strong id' – The Man Apr 06 '12 at 17:10
  • @JoeBurnett You need to cast the sender to UIButton, since it's visible to the method with the type `id`. See my edit. – Moshe Apr 06 '12 at 17:51
  • Would it not be more efficient to do a pointer comparison in your last example as you want to know if it is the exact same object e.g. pointing to the same address – Paul.s Apr 06 '12 at 21:38
  • @Paul.s You may be right, I was just illustrating the basics. :-) – Moshe Apr 06 '12 at 21:46
2

Give each button a unique tag value. in the IBAction, sender.tag tells you which button was tapped.

meggar
  • 1,219
  • 1
  • 10
  • 18
  • hmm... try (IBAction)the_action:(UIButton*)sender... and then [sender tag] – meggar Apr 06 '12 at 17:02
  • tag is a property inherited from UIView, it's an int. This will work. Set your UIButton's tag value in IB or in code with `button.tag = 501`; set the UIButton's action to your handler. Change the parameter type to UIButton* so `-(IBAction)the_handler:(UIButton*)sender`. Now, `int x = [sender tag];` should work. – meggar Apr 06 '12 at 17:20
  • Using a tag is better than using the title. The title may change with a GUI update or with localization. – Ken Thomases Apr 07 '12 at 07:01
1

The IBAction routine you set up to handle the button presses has a sender parameter. Examine that to decide.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
0

That's quite simple, but since you're new, here's an answer.
(According to Stanford cs193p course, 2010-2011 fall (that's what they did with the calculator app)) make a method that receives an argument, which is the UIButton. for example:

- (IBAction) someMethodThatDoesSomething:(UIButton *)sender;

Then make if statements according to the sender.titleLabel.text
I don't know if there are any other solutions. Hope this helps!

Artem Shmatkov
  • 1,434
  • 5
  • 22
  • 41
TheNavigat
  • 864
  • 10
  • 30
0
-(IBAction) buttonPress: (id) sender {
    UIButton *pressedButton = (UIButton *)sender;
    NSString *buttonTitle = [pressedButton currentTitle];
    if ([buttonTitle isEqualToString: @"SomeTitle"]) {
        //do work for that button.
    }
}

You can use a variety of NSString methods to compare or filter which button was pressed and handle it through if's or switches.

Jared Kipe
  • 1,189
  • 6
  • 5
0

-(IBAction)myButtonAction:(id)sender {

    if ([sender tag] == 0) {
        // do something here
    }
    if ([sender tag] == 1) {
        // Do some think here
   }

}

// in Other words

-(IBAction)myButtonAction:(id)sender {

     NSLog(@"Button Tag is : %i",[sender tag]);

    switch ([sender tag]) {
    case 0:
        // Do some think here
        break;
    case 1:
       // Do some think here
         break;
   default:
       NSLog(@"Default Message here");
        break;

}

BigAppleBump
  • 1,096
  • 12
  • 26