Problems with UINavigationController inside of UITabBarController, viewWillAppear not called

╄→гoц情女王★ 提交于 2019-12-03 00:25:33

To answer my own question, I found out what the problem was.

In order to abide by Apple's "No UITabBarController inside of a UINavigationController" I wrote my own tab bar controller (bookTabBarController) which is based off of a standard View Controller. My problem was that the class wasn't passing viewDidAppear down to the class that managed the view controllers, so it never knew it was being shown or not.

Another solution is to set the navigation controller's delegate. Within the delegate, implement the following method:

- (void)navigationController:(UINavigationController *)navigationController  willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [viewController viewWillAppear:animated];
}

This will ensure that viewWillAppear will get called on any view controller whose view is about to appear in the navigation controller. If you do it this way, viewWillAppear gets called regardless of whether the view is appearing because it is being pushed, or it is appearing because a subview is being popped.

A solution to this problem is to have the UIViewController containing the UINavigationController pass the desired messages to it. The UINavigationController will forward the messages to the appropriate view controller. It seems counterintuitive but it works.

@interface NavigationWrapperViewController : UIViewController {
    // navigationController must be a subview of this view controller's view
    UINavigationController *navigationController;
}
@property (nonatomic, assign) UINavigationController *navigationController;
@end

@implementation NavigationWrapperViewController
@synthesize navigationController;

-(void)viewWillAppear:(BOOL)animated {
    [navigationController viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated {
    [navigationController viewDidAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated {
    [navigationController viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated {
    [navigationController viewDidDisappear:animated];
}

@end

You can find a more complete solution on Pastebin (which I did not post).

Thanks to davidbenini.it and jaekwon for this solution.

cloudage

An even simpler trick:

In your subclass of UITabBarController, override this:

-(void)loadView{

    [super loadView];

    //here goes the trick:
    [self setSelectedIndex:1];
    [self setSelectedIndex:0];
}

OK, this is old, very old, but I ended up here with a similar problem.

  UITabViewController
     UINavigationController
       UITableViewController1
         UITableViewController2

When popping out of the UITableViewController2, the viewWillAppear function in UITableViewController1 was never called.

The problem: My UITabViewController custom class was overriding viewWillAppear without calling the super implementation.

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