问题
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:
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.
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/622072/objective-c-iboutlets