Objective C syntax for message passing
I'm trying following task:
I know we can calla method on button click by using:
[button addTarget:self action:@selector(performModalActionForButton)
forControlEvents:UIControlEv开发者_C百科entTouchUpInside];
Method is like :
(void) performModalActionForButton:(NSString *)str
{
}
I want to send a string on button click to one method. How can I achieve this?
Thanks
there are two small mistakes in your code..
if you use
@selector
and want to choose a function with a parameter you have do add a : ... so it is@selector(performModalActionForButton:)
the object passed to the function always the sender of the message .. so the function would be:
- (void) performModalActionForButton:(UIButton *)button { }
In the function you can check the text on the button then... or the tag or whatever you want.
You have the button click trigger an action method that then sends the message whose argument is the string you want to send:
[button addTarget:self action:@selector(performModalActionForButton:)
forControlEvents:UIControlEventTouchUpInside];
- (IBAction)performModalActionForButton:(id)sender
{
NSString *string = /* get the string you want to send */;
[obj doSomethingWithString:string];
}
精彩评论