问题描述
我有一个 UINavigationController
.我必须从 UINavigationController
弹出一个视图并将其替换为另一个视图.我们如何搜索 UIViewController
对象并将其替换为另一个对象?
I have a UINavigationController
. I have to pop a view from a UINavigationController
and replace it with another view. How we can search for a UIViewController
object and replace it with another ?
当我打印时
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: myDelegate.navigationController.viewControllers];
我试过了..
[allViewControllers removeObjectIdenticalTo: @"NonLogginedViewController"];
[allViewControllers removeObjectIdenticalTo: myDelegate.nonLogginedViewController];
myDelegate.navigationController.viewControllers = allViewControllers;
但它没有更新 UINavigationController
堆栈..我不知道如何将 removeObjectIdenticalTo
与 UINavigationController
堆栈一起使用..
But it didn't update the UINavigationController
stack ..
I don't know how to use removeObjectIdenticalTo
with UINavigationController
stack..
请帮帮我..
推荐答案
首先,你的测试:
Firstly, your test:
[allViewControllers removeObjectIdenticalTo:@"NonLogginedViewController"];
[allViewControllers removeObjectIdenticalTo: @"NonLogginedViewController"];
...正在测试字符串,而不是视图控制器.所以这行不通.
...is testing for a string, not a view controller. So that won't work.
如果您知道视图控制器在导航控制器堆栈中的位置,那么这很容易.例如,您刚刚推送了一个新控制器,现在您想删除之前的控制器.你可以这样做:
If you know where the view controller is in the navigation controller's stack then this is easy. Say for example you've just pushed a new controller and now you want to remove the one before that. You could do this:
NSMutableArray *allControllers = [self.navigationController.viewControllers mutableCopy];
[allControllers removeObjectAtIndex:allControllers.count - 2];
[self.navigationController setViewControllers:allControllers animated:NO];
但我认为在您的情况下,您想找到某个控制器并将其删除.一种方法是寻找某个类,例如登录控制器.通过复制旧数组来设置一个新数组,然后遍历这个新数组:
But I think in your case you want to find a certain controller and remove it. One way to do this would be to look for a certain class, e.g. LoginController. Set up a new array by copying the old one, and then iterate through this new array:
NSArray *allControllersCopy = [allControllers copy];
for (id object in allControllersCopy) {
if ([object isKindOfClass:[LoginController class]])
[allControllers removeObject:object];
}
...然后像以前一样为 viewControllers 属性设置 allControllers 数组.
...then set the allControllers array for the viewControllers property, as before.
注意:如果您从包含视图控制器操作 UINavigationController
的堆栈——也许一旦该视图控制器出现——你应该等到视图完全显示在屏幕上.一个很好的地方是在 viewDidAppear:
覆盖,例如:
NOTE: If you're manipulating a UINavigationController
's stack from a containing view controller – perhaps once that view controller appears – you should wait until the view is fully onscreen. A good place for that is in the viewDidAppear:
override, for example:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSMutableArray *allControllers = ...
}
这篇关于如何检查 uiviewcontroller 是否存在于 uinavigationcontroller 堆栈中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!