【问题标题】:NSTimer selector method not getting accessed from another classNSTimer 选择器方法未从另一个类访问
【发布时间】:2012-01-18 22:15:32
【问题描述】:

我在 sample.m 中定义了一个类(Sample),其中我有以下方法:

-(NSInteger)refreshValue:(id)sender{
//Do some thing here and return value.
  NSLog(@"Print something here");
  return something;
}

-(IBAction)monitorValue:(id)sender {
   NSLog(@"In here");
    timer = [NSTimer scheduledTimerWithTimeInterval: 1
                 target: self
                     selector: @selector(refreshValue:)
                 userInfo: nil
             repeats: YES];

}

现在我有另一个课程,test.m。从 test.m,我这样做:

Sample *test = [Sample alloc]
[test monitorValue:(id)sender]

当我运行这段代码时,我可以看到监控值方法被调用。但是 refreshValue 方法永远不会从 monitorValue 中被调用,这正是我想要的。如果我从 sample.m 调用 monitorValue,那么一切正常。当从不同的对象调用时,它只是不起作用。

有什么想法吗?谢谢。

【问题讨论】:

  • 不确定是否有帮助,但将参数更改为 scheduledTimerWithTimeInterval11.0f(它是一个 NSTimeInterval -- 或双 -- 值)。另外,您是否在每次按下该按钮时启动一个全新的计时器?您可能应该检查一下timer 不为空。
  • 修复它:[test performSelectorOnMainThread:@selector(monitorValue:) withObject:nil waitUntilDone:NO];还是谢谢。

标签: objective-c cocoa selector nstimer


【解决方案1】:

您的计时器回调方法签名不正确。根据documentation,应该是:

- (void)timerFireMethod:(NSTimer*)theTimer

【讨论】:

  • 不是这个原因。计时器只是调用选择器,他们不关心方法的签名是什么。这个特殊的refreshValue: 签名完全可以被计时器调用而不会崩溃或发生任何事情。问题的真正原因一定在其他地方。
猜你喜欢
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 2017-05-12
  • 2012-03-19
  • 2015-01-21
  • 2010-12-08
  • 2016-10-21
  • 2014-10-10
相关资源
最近更新 更多