Get Object ID of UI element from code
I'm trying link 40 buttons to one event handler method and then do the processing work from one method instead of 40. I know how to link it but once I get there I don't know how to distinguish what button was pressed. Does anyone know how to reference the button object sender id or is there a开发者_C百科nother way to solve this problem? Any help would be greatly appreciated.
For understand witch button was pressed you need first to create an IBoutlet into you code for each button and then compare it with sender
just using the ==
operator.
Example:
//test.h
@interface Test : UIViewController {
UIButton *button1,button2;
}
@property(nonatomic,retain) IBOutlet UIButton *button1;
@property(nonatomic,retain) IBOutlet UIButton *button1;
-(IBAction)click:(id)sender;
Compare:
//test.m
-(void)click:(id)sender
{
if (button1 == sender)
{ NSLog(@"Button 1"); }
else if (button2 == sender)
{ NSLog(@"Button 2"); }
}
精彩评论