我有一个带有不同ViewControllers的NavigationController。有时我想将ViewController推送到已包含的NavigationController。 在这种情况下,我想从TopController移动到现有的,并在顶部和现有的之间弹出所有其他ViewControllers。
有没有办法给ViewController一个特殊的id(例如NSString),以便稍后在" NavigationController.viewControllers"? 或者我应该使用单独的词典来管理我的ViewControllers吗?
或者有更好的方法,我不考虑。
最好的问候
答案 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)
现在您知道要删除哪个唯一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 })