美文网首页
NavigationController 返回的问题之一

NavigationController 返回的问题之一

作者: FlowYourHeart | 来源:发表于2017-09-12 19:42 被阅读69次

navigationController 想要push到谁,就创建一个VC 就好,很任性😯,这个所有人都知道的事,咱就不说了,咱来说说pop想像push一样任性可好😄

其实咱不任性

在网上一查,因为关键字的原因,一直在 navigationController 拦截返回事件中不能自拔,如果要在

-(void)viewWillDisappear:(BOOL)animated  
{  
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound){  
        // 这里处理 pop 你会发现 出现了一些不太美妙的画面,还是慢动作
        [super viewWillDisappear:animated];  
    }  
} 

看到这里,会不会觉 ~~大招~~将要出来了,其实没有大招😯

1、

如果你要pop的vc是navi的rootViewController,问题就非常简单了,
[self.navigationController popToRootViewControllerAnimated:YES];

2、

遍历navi的子VC

for (id vc in self.navigationController.childViewControllers) {
        if ([vc isKindOfClass:[DDDDDD Class]]) {
    // 一直在想这样 真的可以么 好像真的可以 但真心不喜欢这样         
   [self.navigationController popToViewController:vc animated:YES];
        }
    }

3、

在某些情况下,如果你知道pop时要跳过多少VC,还可以这样

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //navibar
   UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回"
                                                                          style:UIBarButtonItemStyleDone
                                                                         target:self
                                                                         action:@selector(rightClickAction)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

//返回响应
- (void)rightClickAction {
    NSUInteger integer = self.navigationController.childViewControllers.count;//integer - 1 = 当前vc index
    UIViewController *vc = [self.navigationController.childViewControllers objectAtIndex:integer - 4];
    [self.navigationController popToViewController:vc animated:YES];

}

更新:(2018.06.01)
有些朋友问,为什么 C返回A 返回过程中B会闪现,据了解,是返回代码位置放错了,不要放在-(void)viewWillDisappear:(BOOL)animated这个方法里,自己监听方法响应,然后执行返回方法,比如自己写的返回按钮,在按钮的响应方法中执行上面代码

还有很多方法,只要知道本质原理是怎样的,你就会有千千万的方法,right ?

相关文章

网友评论

      本文标题:NavigationController 返回的问题之一

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