开发者

performSelector:withObject:afterDelay in viewDidAppear is not working when app launches

I have to move some code from viewDidAppear to a new selector to fire after 0.1f seconds. So I have done something like:

-(void) viewDidAppear:(BOOL)animated{
    [self performSelector:@selector(startShowing) withObject:nil afterDelay:0.1f];      
}

-(void) startShowing{
    NSLog(@"start Showing");
    ............
}

When I start the application,开发者_Go百科 nothing happens, "start Showing" is not appearing. If I change the view and then come back to the first one, it is working.

Anybody know why?


The problem is that the delay relies on the underlying implementation of NSTimer, which relies on an NSRunLoop, which is instantiated by the time the application finishes launching.

From the Threading Programming Guide:

"When performing a selector on [a] thread, the target thread must have an active run loop. For threads you create, this means waiting until your code explicitly starts the run loop. Because the main thread starts its own run loop, however, you can begin issuing calls on that thread as soon as the application calls the applicationDidFinishLaunching: method of the application delegate. The run loop processes all queued perform selector calls each time through the loop, rather than processing one during each loop iteration."

So check and see if applicationDidFinishLaunching: has fired yet. You said you had just started up the application. I bet it's not done launching yet.

If that's the problem you could fix it a few ways. The simplest to me is to call performSelector:withObject:afterDelay: from applicationDidFinishLaunching.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜