有关UIViewController函数调用与否的测试

问题:当UIViewController的view通过add的方式添加到其他controller上时,导致viewWillAppear等这些函数不被调用

solve:当ios的版本从5.0开始之后,UIViewController的view不管是通过何种方式增加到其他的controller上,viewWillAppear、viewDidAppear等这些函数都会执行,只有5.0以下的版本才需要做特殊处理


特殊处理如下:
     只需要在UINavigationController的根controller中实现就行

.h文件,实现UINavigationControllerDelegate
@interface DPayGameShopViewController : UIViewController<UINavigationControllerDelegate>

.m文件:
- (void)viewDidLoad {
    [super viewDidLoad];
     //判断系统版本
    if ([[DPayConfig sharedInstance] isLowIOSVersion] && nil == self.navigationController.delegate) {
        self.navigationController.delegate = self;
    }
}

- (void)dealloc {
    self.navigationController.delegate = nil;
    [super dealloc];
}

#pragma mark - UINavigationControllerDelegate method

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
     //此处还可以调用viewController其他的类方法  如viewDidAppear等
    [viewController viewWillAppear:animated];
}

然后对应每个页面只要实现viewWillAppear这些方法即可
原文地址:https://www.cnblogs.com/cnsec/p/11515851.html