当在 classA 中使用 NSTimer 时,无法从其他地方调用任何方法
这是我调用的运行计时器的代码,用于调用 classA 中的“tick:”方法(它不在主线程中调用):
- (id)init {
self = [super init];
if (self != nil) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f
target:self selector:@selector(tick:) userInfo:nil repeats:YES];
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
[currentRunLoop run];
}
return self;
}
计时器工作得很好。 但是,当尝试在同一线程中访问“doSomething”方法(类classA的同一实例)时,方法“doSomething”不会调用。
为什么?如何解决这个问题?
Here is a code I call to run timer for calling "tick:" method in classA (it calls NOT in main thread):
- (id)init {
self = [super init];
if (self != nil) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f
target:self selector:@selector(tick:) userInfo:nil repeats:YES];
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
[currentRunLoop run];
}
return self;
}
And timer works great.
But when try to access to "doSomething" method (the same instance of class classA) in the same thread then method "doSomething" does't calls.
Why? How to fix the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何调用选择器
doSomething:
?无论如何,你应该尝试
[classAinstance PerformSelectorOnMainThread:@selector(doSomethingOnMainThread:) waitUntilDone:YESORNO];
How selector
doSomething:
is called?Anyway you should try
[classAinstance performSelectorOnMainThread:@selector(doSomethingOnMainThread:) waitUntilDone:YESORNO];