grouser
grouser

Reputation: 628

iOS: Using the same UIView for five buttons

I have a form with five different UIbutton instances. When I push one, I have to show different options, so my problem is, How can I know which button I have pushed using the same UIView? I don't want to create five UIViews. Is it possible give an ID to the buttons?

Upvotes: 0

Views: 119

Answers (3)

Deepak
Deepak

Reputation: 348

Try this...

(IBAction)nextHipoacusia:(id)sender
 { 
  int currentSender = ((UIButton *)sender).tag;
 }

Upvotes: 0

user529758
user529758

Reputation:

As @Florent pointed out, you can use a tag. But even this complication is not needed - you can directly compare the button objects themselves.

- (void)buttonClicked:(UIButton *)sender
{
    if (sender == button1) {
        // button 1 clicked
    } else if (sender == button2) {
        // etc.
    }
}

Upvotes: 1

Florent Alexandre
Florent Alexandre

Reputation: 136

You can assign a tag to each UIButton in this manner :

[button setTag:4] 

or you can set it in Interface Builder in the View attributes of the UIButton.

Then in your IBAction :

- (IBAction)message:(id)sender 
{
  int currentSender = [sender tag];
  switch(currentSender) {
  // Different actions
  }
}

Upvotes: 1

Related Questions