美文网首页swift赶脚者Ios@IONIC程序员
开发中解决viewWillAppear失效问题

开发中解决viewWillAppear失效问题

作者: 480a52903ce5 | 来源:发表于2016-06-27 13:22 被阅读3732次
    开发中解决viewWillAppear失效问题
    今天写个小demo出现viewWillAppear失效问题,于是就就找度娘解决了这个问题,还是度娘好,这里先给度娘点赞一下,废话不多说了,问题具体原因:
    UINavigationController. 将UINavigationController的view作为subview添加到了其他viewController的view中。或者把UINavigationController添加到UITabbarController中了;
    此时,NavigationController的stack里面的viewController就收不到-(void)viewWillAppear:(BOOL)animated等4个方法的调用
    错误报告:
    [Warning: If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly. Failing to send the view controller this message will prevent any associated animation from being displayed]
    看着英文头都晕+_+, 也不翻译了,具体解决这个问题有两种方法:

    第一种:在viewWillAppear与viewWillDisappear把你的subCntlr的里面这两方法在调用一次即可--

    -(void)viewWillAppear:(BOOL)animated
    {
     [super viewWillAppear:animated];
    [subCntlr viewWillAppear:animated];
    
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
     [super viewWillDisappear:animated];
     [subCntlr viewWillDisappear:animated];
    }
    

    第二种:把导航控制器上层controller设为UINavigationController的delegate--

    nav.delegate = self;
    @interface 
    RootViewController : UIViewController <UINavigationControllerDelegate> {
        UINavigationController *navController;
    }
     
    - (void
    )navigationController:(UINavigationController *)navigationController 
        willShowViewController:(UIViewController *)viewController animated:(BOOL
    )animated 
    {
        [viewController viewWillAppear:animated];
    }
    
    - (void
    )navigationController:(UINavigationController *)navigationController 
        didShowViewController:(UIViewController *)viewController animated:(BOOL
    )animated 
    {
        [viewController viewDidAppear:animated];
    }
    

    相关文章

      网友评论

      本文标题:开发中解决viewWillAppear失效问题

      本文链接:https://www.haomeiwen.com/subject/rgpzdttx.html