可能重复:
Arguments in @selector
如何调用带有2个以上参数的参数化方法到选择器, 对EX说我有这样的方法
-(void)GetTheData:(NSString *)str1 :(NSString *)str2
现在我需要在@ selector中的以下计时器中调用此方法。我可以调用吗?
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(**HowCanICallHere**) userInfo:nil repeats:YES];
答案 0 :(得分:2)
将NSDictionary
作为参数传递给目标方法
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"parameter1",@"2",@"parameter2", nil];
[ NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(myFunction:) userInfo: dictionary repeats:NO];
进一步检索目标函数中的参数
-(void)myFunction:(NSTimer *)timer{
NSLog(@" dict : %@",timer.userInfo);
}
这样,您可以通过在NSDictionary
答案 1 :(得分:2)
我不确定你能不能。出于同样的原因,我们给出了“userInfo”选项,以传递多个参数。您可以通过创建包含两个对象的字典轻松实现它:
NSDictionary *dict = [NSDictionary dictionaryWithObjects:objArray andKeys:keyArray];
并将该字典作为userInfo对象传递给方法:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(urTimerMethod:) userInfo:dict repeats:YES];
将方法定义为:
- (void)urTimerMethod:(NSTimer *)timer {
NSDictionary *dict = [timer userInfo];
}
答案 2 :(得分:0)
您可以使用:
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(GetTheData::) userInfo:nil repeats:YES];
答案 3 :(得分:0)
我知道如何将它与执行选择器一起使用,所以也许你可以通过以下方式实现它:
-(void)GetTheData1:(NSString *)str1 GetTheData2:(NSString *)str2
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(mymethod) userInfo:nil repeats:YES];
- (void) mymethod {
[self performSelector:@selector(GetTheData1:GetTheData2:) withObject:str1 withObject:str2];
}