Passing arguments to selector
i have been working on a small demo and i wrote a function which accepts two string object and looks like this
-(void)adding:(NSString*)num1 :(NSString*)num2
{
开发者_如何学运维 num1 = first.text; //first is object of label
num2 = second.text; //second is object of label
int x= [num1 integerValue];
int y = [num2 integerValue];
int r = x+y;
NSLog(@"%d",r);
}
am having one more function
-(void)calling
{
[self performSelector:@selector(adding:)withObject:@"num1" withObject:@"num2"];
}
and am calling the method named calling on button touch up inside event
[btn addTarget:self action:@selector(calling) forControlEvents:UIControlEventTouchUpInside];
but i get an error called unrecognized selector sent to instance
can you please tell me where i am wrong and how to pass methods with arguments to selector. Thank you
You're missing a colon. Try this :
[self performSelector:@selector(adding::)withObject:@"num1" withObject:@"num2"];
However, it's bad form to have unnamed parameters on your method names because errors like this happen ;)
I'd rename your method to be
-(void)adding:(NSString*)num1 to:(NSString*)num
and have this line :
[self performSelector:@selector(adding:to:)withObject:@"num1" withObject:@"num2"];
You got your selector wrong, it should be @selector(adding::)
Also in this case you don’t need to use performSelector:...
, you can send the message directly: [self adding: @"num1" : @"num2"]
.
精彩评论