使用特殊键在NavigationController中查找ViewController

时间:2014-10-16 14:49:19

标签: ios objective-c iphone uiviewcontroller

我有一个带有不同ViewControllers的NavigationController。有时我想将ViewController推送到已包含的NavigationController。 在这种情况下,我想从TopController移动到现有的,并在顶部和现有的之间弹出所有其他ViewControllers。

有没有办法给ViewController一个特殊的id(例如NSString),以便稍后在" NavigationController.viewControllers"? 或者我应该使用单独的词典来管理我的ViewControllers吗?

或者有更好的方法,我不考虑。

最好的问候

3 个答案:

答案 0 :(得分:4)

在您的appDelegate上,您可以存储所有ViewController以及何时要弹出调用:

[self.navigationController popToViewController:yourViewController animated:YES];

或者您可以尝试搜索

NSArray *viewControllers = self.navigationController.viewControllers 
for (UIVIewController *anVC in viewControllers) {
    if (anVC isKindOfClass:[yourController class] {
        [self.navigationController popToViewController:anVC animated:YES];
        break;
    }
}

希望它会对你有所帮助。

答案 1 :(得分:1)

注意:由于视图控制器是同一类,大多数时候

在每个控制器上添加strUniqueID属性,同时将视图控制器推送到导航控制器,不要忘记设置

现在您知道要删除哪个唯一ID,所以找到它

//Firstly find your viewController
for(id viewcontroller in self.navigationController.viewControllers)
{
     //For finding specific viewController use isKindOfClass
     if(viewcontroller isKindOfClass:[YourViewControllerNameHere Class])
     {
         //Now find UniqueIDHere
         YourViewController *objYourViewController = (YourViewController *)viewcontroller
         if(YourViewControllerNameHere.strUniqueID isEqualToString:removeUniqueIDHere])
         {
             //Now pop to YourViewController
             [self.navigationController popToViewController:viewcontroller animated:YES];
             break;
         }

     }
     else if(viewcontroller isKindOfClass:[YourDifferentViewControllerNameHere Class]) //Different ViewControllers here like this
     {
         //Now find UniqueIDHere
         YourDifferentViewController *objYourDifferentViewController = (YourViewController *)viewcontroller
         if(YourDifferentViewController.strUniqueID isEqualToString:removeUniqueIDHere])
         {
             //Now pop to YourViewController
             [self.navigationController popToViewController:viewcontroller animated:YES];
             break;
         }
     }

}

试试这个:

//Firstly find your viewController
for(id viewcontroller in self.navigationController.viewControllers)
{
     //For finding specific viewController use isKindOfClass
     if(viewcontroller isKindOfClass:[YourViewControllerNameHere Class])
     {
         //Now pop to YourViewController
         [self.navigationController popToViewController:viewcontroller animated:YES];
         break;
     }
}

答案 2 :(得分:0)

快捷键5

let vc = navVC.viewControllers.first(where: { $0.hasKey })