Why does viewDidAppear in UITabBarController exec before the view appears?

时间秒杀一切 提交于 2019-11-29 15:37:16

问题


I have a UITabBarController that nests a UIView-Subclass (ImageViewer) as it's third tab.

In this ImageViewer Subclass I call the viewDidAppear method:

- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    /* ... start custom code ... 
    NSLog(@"viewDidAppear tag 1 passed);          /* BREAKPOINT 1 here

    [myUIActivityIndicator stopAnimating];

    NSLog(@"viewDidAppear tag 2 passed);          /* BREAKPOINT 2 here
    /* ... end custom code ... 
}

the method is called automatically, but strangely the view only appears after this method has been processed completely?

When I set breakpoints (1 and 2) as indicated, the processing (upon selecting the tab) stops whilst the previous tab is still showing. Only when clicking continue after the second breakpoint, the view will be displayed. (FYI the NSLogs are carried out immeldiately).

In this case viewDidAppear behaves more like viewWillAppear ....

Any clues what might be going on?

Cheers


回答1:


If you want to allow the screen to be re-drawn when your view loads, but to trigger some other updating code in -viewDidAppear:, use performSelector:withObject:afterDelay: like this:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self performSelector:@selector(updateUI) withObject:nil afterDelay:0.0];
}

…

- (void)updateUI
{
    // Do your UI stuff here
}

When you do it this way, the current event loop will finish quickly, and UIKit will be able to re-draw the screen after your view has loaded. updateUI will be called in the next event loop. This is a good way to get snappy view transitions if you have to perform computationally intensive calculations or updates after a view has loaded.




回答2:


From the sound of it, if you are actively calling the method, the device might not have time to actually display the view while it is running the "custom code" in your viewDidAppear method. I that case you should let the program call the viewDidAppear method itself.

Your program may also be working on other code which would slow down the appearance of the view, this can be solved using timers. i.e. instead of:

[self otherCode];

you would write:

[NSTimer scheduledTimerWithTimeInterval:.5 
    target:self 
    selector:@selector(otherCode) 
    userInfo:nil 
    repeats:NO];

you might want to try simply delaying your "custom code" with a timer in this way.



来源:https://stackoverflow.com/questions/4297436/why-does-viewdidappear-in-uitabbarcontroller-exec-before-the-view-appears

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!