@selector-有多个参数? [英] @selector - With Multiple Arguments?
问题描述
我今天第一次使用@selector
,还无法确定如何执行以下操作?如果您有多个参数,您将如何编写@selector
?
I have been using @selector
today for the first time and have not been able to work out how to do the following? How would you write the @selector
if you had more than one argument?
没有参数:
-(void)printText {
NSLog(@"Fish");
}
[self performSelector:@selector(printText) withObject:nil afterDelay:0.25];
单个参数:
-(void)printText:(NSString *)myText {
NSLog(@"Text = %@", myText);
}
[self performSelector:@selector(printText:) withObject:@"Cake" afterDelay:0.25];
两个参数:
-(void)printText:(NSString *)myText andMore:(NSString *)extraText {
NSLog(@"Text = %@ and %@", myText, extraText);
}
[self performSelector:@selector(printText:andMore:) withObject:@"Cake" withObject:@"Chips"];
多个参数:(即大于2个)
Multiple Arguments: (i.e. more than 2)
推荐答案
- (id)performSelector:(SEL)aSelector
withObject:(id)anObject
withObject:(id)anotherObject
从文档:
此方法与 performSelector相同:,除了可以为aSelector提供两个参数. aSelector应该确定一个可以接受两个类型为id的参数的方法.对于具有其他参数类型和返回值的方法,请使用 NSInvocation .
This method is the same as performSelector: except that you can supply two arguments for aSelector. aSelector should identify a method that can take two arguments of type id. For methods with other argument types and return values, use NSInvocation.
因此,在您的情况下,您将使用:
so in your case you would use:
[self performSelector:@selector(printText:andMore:)
withObject:@"Cake"
withObject:@"More Cake"]
这篇关于@selector-有多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!