Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text of button from IBAction - iPhone

When an IBAction is called:

-(IBAction) onClick1: (id) sender; 

What is passed in the sender? Since it's hooked up through the IB, I'm not really sure. My question is how to get the text of the button to be the passed object (NSString most likely) so that I could call it inside the action implementation.

-(IBAction) onClick1: (id) sender {   NSLog(@"User clicked %@", sender);   // Do something here with the variable 'sender' } 
like image 484
Organiccat Avatar asked May 20 '09 15:05

Organiccat


1 Answers

The sender should be the control which initiated the action. However, you should not assume its type and should instead leave it defined as an id. Instead, check for the object's class in the actual method as follows:

- (IBAction)onClick1:(id)sender {     // Make sure it's a UIButton     if (![sender isKindOfClass:[UIButton class]])         return;      NSString *title = [(UIButton *)sender currentTitle]; } 
like image 65
Matt Ball Avatar answered Sep 16 '22 22:09

Matt Ball