使用  NSTimer scheduledTimerWithTimeInterval: target: selector:userInfo: repeats: 的时候有两个地方需要注意 。

  1. 首先selector指定的方法必须是带一个参数的方法,并且那个参数的类型是NSTimer *。
  2. 其次,参数是靠NSTimer 对象的userInfo属性来传递的。

举个例子:

[NSTimer scheduledTimerWithTimeInterval:0.5 
                       target:self 
                       selector:@selector(sendBroadcast:) 
                       userInfo:@"hello I'm the info to send" 
                       repeats:NO];
// sendBroadcast的定义形式应该是
-(void)sendBroadcast: (NSTimer *)timer { 

   NSString *msg = (NSString *)[timer userInfo]; // [msg isEqualToString @"hello I'm the info to send"] == YES
}

 

 posted on 2014-12-11 00:12  子叶_Sky  阅读(2216)  评论(0编辑  收藏  举报