2010-10-25 59 views
55

如何将参数传递给由NSTimer调用的方法?我的计时器看起来像这样:将参数传递给由NSTimer调用的方法

[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(updateBusLocation) userInfo:nil repeats:YES]; 

我希望能够将字符串传递给方法updateBusLocation。此外,我应该在哪里定义方法updateBusLocation?在创建计时器的同一个.m文件中?

编辑:

其实我仍然有问题。我收到错误消息:

终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因是: '* - [MapKitDisplayViewController updateBusLocation]:无法识别的选择发送到实例0x4623600'

这里是我的代码:

- (IBAction) showBus { 

//do something 

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateBusLocation) userInfo:txtFieldData repeats:YES]; 
[txtFieldData release]; 
} 


- (void) updateBusLocation:(NSTimer*)theTimer 
{ 
     NSLog(@"timer method was called"); 
     NSString *txtFieldData = [[NSString alloc] initWithString:(NSString*)[theTimer userInfo]]; 
if(txtFieldData == busNum.text) { 
    //do something else 
    } 
    } 

编辑#2: 没关系您的示例代码工作正常感谢您的帮助。

+0

,即时通讯肯定一吨的人在一个点或另想知道固体问题。谢谢! – 2013-02-14 01:25:14

回答

95

您需要在目标中定义方法。既然你将目标设置为“自我”,那么是的,同一个对象需要实现该方法。但是你可以将目标设定为你想要的任何东西。

userInfo是一个指针,您可以将它设置为您喜欢的任何对象(或集合),并在计时器激发时传递给目标选择器。

希望有所帮助。

编辑:...简单的例子:

设置定时器:

NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:2.0 
           target:self 
           selector:@selector(handleTimer:) 
           userInfo:@"someString" repeats:NO]; 

并实现在同一类的处理程序(假设你的目标设定为 '自我'):

- (void)handleTimer:(NSTimer*)theTimer { 

    NSLog (@"Got the string: %@", (NSString*)[theTimer userInfo]); 

} 
+0

我仍然有点失落你会介意给我一个计时器的例子,它调用一个方法并将参数传递给该方法吗? – bubster 2010-10-25 01:31:12

+0

当然,我用一个简单的例子编辑了我的答案 – 2010-10-25 01:56:37

22

你可以通过用户信息传递你的论点:[NSDictionary dictionaryWithObjectsAndKeys:parameterObj1, @"keyOfParameter1"];

一个简单的例子:

[NSTimer scheduledTimerWithTimeInterval:3.0 
           target:self 
           selector:@selector(handleTimer:) 
           userInfo:@{@"parameter1": @9} 
           repeats:NO]; 

- (void)handleTimer:(NSTimer *)timer { 
    NSInteger parameter1 = [[[timer userInfo] objectForKey:@"parameter1"] integerValue]; 
} 
1

对于斯威夫特做到这样,

例如,你想要使用发送的UILabel的NSTimer

override func viewDidLoad() { 
    super.viewDidLoad() 

    var MyLabel = UILabel() 
    NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("callMethod:"), userInfo: MyLabel, repeats: false) 
} 


func callMethod(timer:NSTimer){ 

    var MyLabel:UILabel = timer.userInfo as UILabel 

} 
2

其他例如斯威夫特字典字面将参数传递给调用的方法的NSTimer

override func viewDidLoad() { 
    super.viewDidLoad() 

    let dictionary: [String : AnyObject] = ["first element" : "Jordan", 
              "second element" : Int(23)] 

    NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(0.41), 
              target: self, 
              selector: "foo:", 
              userInfo: dictionary, 
              repeats: false) 
} 

func foo(timer: NSTimer) { 
     let dictionary: [String : AnyObject] = timer.userInfo! as! [String : AnyObject] 
     let firstElement: String = dictionary["first element"] as! String 
     let secondElement: Int = dictionary["second element"] as! Int 
     print("\(firstElement) - \(secondElement)") 
}