开发者

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

开发者 https://www.devze.com 2023-04-05 08:46 出处:网络
Here is a code I call to run timer for calling \"tick:\" method in classA (it calls NOT in main thread):

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"开发者_如何学JAVA does't calls.

Why? How to fix the problem?


How selector doSomething: is called?

Anyway you should try [classAinstance performSelectorOnMainThread:@selector(doSomethingOnMainThread:) waitUntilDone:YESORNO];

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号