Why does viewDidAppear in UITabBarController exec before the view appears?

前端 未结 2 1449
悲哀的现实
悲哀的现实 2021-01-01 05:42

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

In this ImageViewer Subclass I call the viewDidAppear method:

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-01 06:14

    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.

提交回复
热议问题