【发布时间】: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,那么一切正常。当从不同的对象调用时,它只是不起作用。
有什么想法吗?谢谢。
【问题讨论】:
-
不确定是否有帮助,但将参数更改为
scheduledTimerWithTimeInterval从1到1.0f(它是一个 NSTimeInterval -- 或双 -- 值)。另外,您是否在每次按下该按钮时启动一个全新的计时器?您可能应该检查一下timer不为空。 -
修复它:[test performSelectorOnMainThread:@selector(monitorValue:) withObject:nil waitUntilDone:NO];还是谢谢。
标签: objective-c cocoa selector nstimer