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

  • 相关阅读:
    使用.Net Core 2.2创建windows服务
    Ubuntu 18.04 安装部署Net Core、Nginx全过程
    Task的在主线程处理异常信息的Helper类
    行转列的处理。
    netcore 下加密遇到的问题
    关于安装angular-cli环境报错的问题
    64位的windows服务安装问题
    EF.Mysql在codefirst模式下调用存储过程,和再DbFirst模式下的调用
    阿里云消息队列的C#使用http接口发送消息实例
    [转载]EF或LINQ 查询时使用IN并且根据列表自定义排序方法
  • 原文地址:https://www.cnblogs.com/langtianya/p/4199409.html
Copyright © 2011-2022 走看看