• 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==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];
    
        
    }

    //调用

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

  • 相关阅读:
    Java泛型类和泛型方法
    Java泛型解析
    Reflection and array
    再叙Java反射
    Java Reflection(getXXX和getDeclaredXXX)
    Java反射(Reflection)
    MySQL Block Nested Loop and Batched Key Access Joins(块嵌套循环和批量Key访问连接)
    jQuery学习之jQuery Ajax用法详解
    request和request.form和request.querystring的区别
    浏览器中event.srcElement和event.target的兼容性问题
  • 原文地址:https://www.cnblogs.com/langtianya/p/4199409.html
Copyright © 2020-2023  润新知