简体   繁体   English

在classA中使用NSTimer时,无法从其他地方调用任何方法

[英]When use NSTimer in classA then can't call any method from other place

Here is a code I call to run timer for calling "tick:" method in classA (it calls NOT in main thread): 这是我调用运行计时器以在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;
    }

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. 但是,当尝试在同一线程中访问“ doSomething”方法(类classA的相同实例)时,则不会调用方法“ doSomething”。

Why? 为什么? How to fix the problem? 如何解决问题?

How selector doSomething: is called? 选择器doSomething:如何被调用?

Anyway you should try [classAinstance performSelectorOnMainThread:@selector(doSomethingOnMainThread:) waitUntilDone:YESORNO]; 无论如何,您应该尝试[classAinstance performSelectorOnMainThread:@selector(doSomethingOnMainThread:) waitUntilDone:YESORNO];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM