zoukankan      html  css  js  c++  java
  • iphone开发之解决viewWillAppear失效

    你可曾遇到过viewWillAppear没有被调用到的情况

    产生原因是用了UINavigationController. 
    将UINavigationController的view作为subview添加到了其他viewController的view中。
    或者把UINavigationController添加到UITabbarController中了。

    此时,NavigationController的stack里面的viewController就收不到-(void)viewWillAppear:(BOOL)animated;等4个方法的调用。

    原因呢

    Apple Docs state:

    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.

    不过后面的到4.0的文档就没有发现这样的文字描述了,但是还是没能够调用的到这样

    只是添加的更复杂的文档,头晕晕看不下去了。

    解决方法两种:
    1,在导航控制器上层controller的viewWillAppear中显式调用viewWillAppear方法。

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

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

    2,把导航控制器上层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];
    }
  • 相关阅读:
    关于html元素Css样式设置的一点心得(特别是与位置有关的,还有外边距、内边距有关的那些)
    【idea的一个安装细节】是不是使用idea不能连接网络了?
    html中a标签属性parent和self的举例说明
    关于jquery的each的操作;
    superagent中文文档
    mongoose 查询子文档的方法
    Object.prototype.toString.call()进行类型判断
    局部函数的直接引用与调用
    数据模型中某个字段是单选或者多选的定义方式;
    nodejs项目中的路由写法
  • 原文地址:https://www.cnblogs.com/gaoxiao228/p/2553519.html
Copyright © 2011-2022 走看看