不同的类会有不同的传递方式,参数名也不尽相同。如果是传单个参数的就不用集合,如果是传多个参数可以用类似nsarray,nsdictionary之类的集合传递。看下面例子:
例子1:
通过NSTimer看IPhone对@selector的函数如何传参数,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; if(oldView != nil) { [dict setObject:oldView forKey:@"oldView"]; } if(newView != nil) { [dict setObject:newView forKey:@"newView"]; } [NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(onTimer:) userInfo:dict repeats:NO]; [dict release]; - (void)onTimer:(NSTimer *)timer { UIView *oldView = [[timer userInfo] objectForKey:@"oldView"]; UIView *newView = [[timer userInfo] objectForKey:@"newView"]; [UIView animateWithDuration:2.0 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ oldView.alpha = 0.0; newView.alpha = 1.0; } }
从上可以看出,NSTimer在对@selector(onTimer:)传递参数时,将传参的对象储存在了NSTimer的userInfo的字典里,在- (void)onTimer:(NSTimer *)timer中
通过取出该字典加以使用。
例子2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
-(
void
)addNotifications:(NSArray*)data{
if
(data==nil||data.count!=2) {
return
;
}
//nsstring字符串转nsinteger
NSInteger notifyNum=[(NSString*)data[0] intValue];
NSInteger index=[data[1] intValue];
MyNBTabButton *button=_buttonData[index];
[button.light addNotifications:notifyNum];
}<br><br>
//调用
|
-(void)addNotificationAfterTime
{
[NSThread sleepForTimeInterval:20];//休眠多少秒之后
[self performSelectorOnMainThread:@selector(addNotifications:) withObject:[NSArray arrayWithObjects:@"1",@"2", nil] waitUntilDone:NO];
[NSThread sleepForTimeInterval:1.0];
}
这个其实也就是iphone对@selector对象传参的通用的形式。
转载请注明:http://www.cnblogs.com/langtianya/p/4199409.html