views:

2674

answers:

3

In cases where multiple buttons call an IBOutlet, can the IBOutlet determine which button was pressed?

edit:

All fixed and wired up. key point: Object ID is not sender tag! Tag is a standalone value on the first page of the attributes.

- (IBAction)buttonPressed:(id)sender
{
    switch ( [sender tag] )
    {
        case 109:
            NSLog(@"Button 1");
            break;

        case 108:
            NSLog(@"Button 2");
            break;
    }
}
+1  A: 

Normally you would connect the buttons event (Touch Down) to the IBAction method you wish to invoke in your controller.

The method prototype would look like:

-(IBAction) doStuff:(id)sender;

Then the "sender" could be used to verify identity of the object doing the calling if needed.

IBOutlet would be used to contact the UIButton from your code, to set its text or properties for example.

Ryan Townshend
-(IBAction)buttonPressed:(id)sender{ switch ( [sender tag] ) { case 109: NSLog(@"Button 1"); break; case 108: NSLog(@"Button 2"); break; }Is the object ID in IB the sender tag? I added the method above w/o effect. I didn't do any particular wiring though. }
Alan
+4  A: 

In cases where multiple buttons call an IBOutlet

maybe an IBAction?

firs if all

- (IBAction) actionPerformed:(id)sender

so, sender is your button

second is that every subclass of UIView has a tag field, so you can use it

int tag = [sender tag];

you can set tag in IB

Igor
A: 

Better still, define an IBAction method for each button in your UI. Then you can individually wire buttons to methods using IB and avoid hard coding const integers into your code.

spymaster