首页 > 代码库 > ios开发@selector的函数如何传参数/如何传递多个参数

ios开发@selector的函数如何传参数/如何传递多个参数

不同的类会有不同的传递方式,参数名也不尽相同。如果是传单个参数的就不用集合,如果是传多个参数可以用类似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:

-(void)addNotifications:(NSArray*)data{    if (data=http://www.mamicode.com/=nil||data.count!=2) {>
//调用

-(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

ios开发@selector的函数如何传参数/如何传递多个参数