美文网首页iOS
ios开发之从某个页面返回后刷新当前页面

ios开发之从某个页面返回后刷新当前页面

作者: 中流者结使也 | 来源:发表于2016-05-03 15:38 被阅读10567次

    开发中总是有从一个界面返回后刷新当前页面的需求,为了减少耦合性,尽量不在当前页面加代码,在返回的界面添加以下方法:

    - (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated;

    此方法是UINavigationControllerDelegate的方法,本类需要遵循UINavigationControllerDelegate,还需设置NavigationController的代理为本类,在ViewWillApear:方法中设置:

    - (void)viewWillAppear:(BOOL)animated{
          [superviewWillAppear:animated];

          self.navigationController.delegate=self; 

    }

    在- (void)navigationController: willShowViewController: animated:方法中添加以下代码:

    - (void)navigationController:(UINavigationController*)navigationController willShowViewController:(UIViewController*)viewController animated:(BOOL)animated{

              if([[viewController class]isSubclassOfClass:[目标类 class]]) {

                ///执行刷新操作

              }

            ///删除代理,防止该controller销毁后引起navigationController.delegate指向野指针造成崩溃

              if(![[viewController class]isSubclassOfClass:[self class]]) {

                 self.navigationController.delegate=nil;

              }

    }

    之前试过在viewWillDisappear:中删除代理,发现viewWillDisappear:会在 -(void)navigationController: willShowViewController: animated:前执行,就不会进入- (void)navigationController: willShowViewController: animated:执行刷新操作了。

    然后又试了在viewDidDisappear:中删除代理,但是会造成该controller销毁后引起navigationController.delegate指向野指针,造成崩溃,不知为什么。

    所以就使用了正文中用到的方法。

    相关文章

      网友评论

        本文标题:ios开发之从某个页面返回后刷新当前页面

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