【问题标题】:How do I pass more than one parameter to the selector in 'scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:'如何将多个参数传递给“scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:”中的选择器
【发布时间】:2012-03-01 11:46:19
【问题描述】:

我知道用户信息用于传递参数,但如何传递多个参数?

我猜我必须使用一个对象,但由于我对 Objective-c 还很陌生,我真的不知道这是否正确以及如何去做?

谢谢!

【问题讨论】:

    标签: objective-c parameters userinfo


    【解决方案1】:

    创建一个包装器对象,一个NSArrayNSDictionary,其中包含您需要传递的多个对象,并在userInfo 中传递该包装器对象。在接收器上从包装器对象中检索对象。

    使用 NSDictionary 作为包装器的示例代码:

    调用代码:

    NSString *obj1 = @"string1"; 
    NSString *obj2 = @"string2"; 
    NSDictionary *wrapper = [NSDictionary dictionaryWithObjectsAndKeys:obj1, @"Object1", obj2, @"Object2", nil];
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:wrapper repeats:NO];
    

    接收定时器代码:

    - (void)timerFireMethod:(NSTimer*)theTimer {
        NSDictionary *wrapper = (NSDictionary *)[theTimer userInfo];
        NSString * obj1 = [wrapper objectForKey:@"Object1"];
        NSString * obj2 = [wrapper objectForKey:@"Object2"];
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 2022-01-18
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多