美文网首页
A—>B—>C,C—>A的两种VC跳转方式

A—>B—>C,C—>A的两种VC跳转方式

作者: lym不解释 | 来源:发表于2017-03-10 11:27 被阅读81次

有A到B再到C,我现在再C页面返回的时候我想直接跳转到A页面 :

1.A push B push C,C pop A;


push.png

对于push比较好的一点,就是有个栈顶控制器navigationController,它装载所有下面的控制器,从viewControllers里面取出A,连续pop2次就可以回到A控制器:

NSArray * viewControllers = weakSelf.navigationController.viewControllers;
    
    for (UIViewController * avc in viewControllers) {
        
        if ([avc isKindOfClass:[UIViewAControllerA class]]) {
            
            [self.navigationController popToViewController:(UIViewController*)avc animated:YES];
        }
    }
  1. A present B present C,C dismiss A
present.png

最简单的方式直接使用通知,在C中dismiss的时候发出通知,在B中监听C然后dismiss到A:

// 在C中关掉自己
    [self dismissViewControllerAnimated:YES completion:^(){
        //关掉注册controller
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_CLOSE_B object:nil userInfo:nil];
    }];

// 在B中添加监视通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(close) name:NOTIFICATION_CLOSE_B object:nil];

-(void) close{
 [self dismissViewControllerAnimated:YES completion:nil];
}

// 移除通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

相关文章

网友评论

      本文标题:A—>B—>C,C—>A的两种VC跳转方式

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