zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    fragment、ListFragment使用ListView及自定义Listview等初始化操作
    【LeetCode】 sort list 单清单归并
    HDU 1251 统计拼图 Trie解决问题的方法
    Oracle推断领域包括中国
    hust1384---The value of F[n]
    ffmpeg参数具体解释
    什么是大数据的核心价值?
    还是畅通project(杭州电1233)
    Redis源代码分析(二十七)--- rio制I/O包裹
    回想一下著名的BigTable论题
  • 原文地址:https://www.cnblogs.com/langtianya/p/4199409.html
Copyright © 2011-2022 走看看