弹出到导航堆栈中的特定viewcontroller [英] Popping to a specific viewcontroller in a navigation stack

查看:76
本文介绍了弹出到导航堆栈中的特定viewcontroller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一段代码要弹出到导航堆栈中的特定viewcontroller,如下所示

I have a come across a piece of code to pop to a specific viewcontroller in a navigation stack as below

for (UIViewController* viewController in self.navigationController.viewControllers) {
    if ([viewController isKindOfClass:[MyGroupViewController class]] ) {
        MyGroupViewController *groupViewController = (MyGroupViewController*)viewController;
        [self.navigationController popToViewController:groupViewController animated:YES];
    }
}

目标是弹出MyGroupViewController。但我不理解这行代码。

The objective is to pop to MyGroupViewController. But I am not understanding this line of code.

MyGroupViewController *groupViewController = (MyGroupViewController*)viewController;

这里到底发生了什么。我不认为这里会创建一个新的MyGroupViewController实例。

Whats exactly happening here. I don't think a new instance of MyGroupViewController is being created here.

推荐答案

//This for loop iterates through all the view controllers in navigation stack.   
for (UIViewController* viewController in self.navigationController.viewControllers) {

    //This if condition checks whether the viewController's class is MyGroupViewController 
    // if true that means its the MyGroupViewController (which has been pushed at some point)
    if ([viewController isKindOfClass:[MyGroupViewController class]] ) {

        // Here viewController is a reference of UIViewController base class of MyGroupViewController 
        // but viewController holds MyGroupViewController  object so we can type cast it here
        MyGroupViewController *groupViewController = (MyGroupViewController*)viewController;
        [self.navigationController popToViewController:groupViewController animated:YES];
    }
}

你也可以这样做

if ([viewController isKindOfClass:[MyGroupViewController class]] ) {
            [self.navigationController popToViewController:viewController  animated:YES];
 }






Swift代码

//Itrate through all the view controllers in navigation stack
for vc in self.navigationController!.viewControllers {
  // Check if the view controller is of MyGroupViewController type
  if let myGropupVC = vc as? MyGroupViewController {
    self.navigationController?.popToViewController(myGropupVC, animated: true)
  }
}

这篇关于弹出到导航堆栈中的特定viewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆