我想从导航堆栈中删除Previous视图控制器。
例如
- > A是根视图。现在导航到B-b导航到c-c导航到c-c导航到c
- >现在我要删除所有c视图控制器&弹出到B
B不是修复视图控制器。
A - > b - > c> g> c> f> c> c> c> ç
从导航中删除所有c视图控制器&需要关注输出
A - > b - > c> g> c> f
请帮帮我
答案 0 :(得分:2)
从导航堆栈中识别控制器。然后弹出那个控制器
NSArray *viewControllers = [[self navigationController] viewControllers];
int count = [viewControllers count];
for (int i= count-2; i >= 0 ; i--) {
id obj=[viewControllers objectAtIndex:i];
if(![obj isKindOfClass:[C class]]){
[[self navigationController] popToViewController:obj animated:YES];
return;
}
}
答案 1 :(得分:0)
您可以使用popToViewController
(https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621871-poptoviewcontroller)。
示例:
目标C
[self.navigationController popToViewController:self.navigationController.viewControllers[your_VC_index] animated: true];
// in first eg your_VC_index will be 1 and another example it will be 5
在Swift 3中
self.navigationController.popToViewController( self.navigationController.viewControllers[your_VC_index], animated: true)
答案 2 :(得分:0)
反向迭代导航堆栈并获取第一个非匹配viewController的索引,然后获取popToViewController
if let controllers = self.navigationController?.viewControllers {
var count = controllers.count - 1
for viewcontroller in controllers.reversed() {
if viewcontroller is CViewController {
count -= 1
} else {
break
}
}
let vc = controllers[count]
self.navigationController?.popToViewController(vc, animated: true)
}
答案 3 :(得分:0)
编写类似以下的方法,
- (void)removeAllLastCObjectsFromNavigationController {
//fetch viewcontroller array. for your case which will be A - > b -> c > g > c > f > c > c > c > c
NSArray *viewControllers = yourNavigationController.viewControllers;
NSMutableArray *mutableArray = [NSMutableArray arrayWithArray: viewControllers];
while(1) {
id viewController = mutableArray.lastObject;
if ([viewController isKindOfClass:[YourCClassName class]]) {
//Now continue removing the objects from the array if that is C type object.
[mutableArray removeLastObject];
} else {
//If found any other class than C stop removing viewcontroller
break;
}
}
//Now mutableArray contains A - > b -> c > g > c > f
yourNavigationController.viewControllers = mutableArray;
}
答案 4 :(得分:0)
因为navigationController的childViewControllers是一个堆栈,所以 所以使用这个函数,你可以弹出到f viewController,“c> c> c> c”被破坏。
for (UIViewController *tempVC in self.navigationController.childViewControllers) {
if ([tempVC isKindOfClass:[fViewController class]]) {
fViewController *f = (fViewController *)tempVC;
[self.navigationController popToViewController:f animated:YES];
}
}
但是,如果你不想弹出到f,只想获得navigationController的新的childViewControllers。你可以使用下一个功能:
for (UIViewController *tempVC in self.navigationController.childViewControllers) {
if ([tempVC isKindOfClass:[ScanViewController class]]) {
NSUInteger findex = [self.navigationController.childViewControllers indexOfObject:tempVC];
NSMutableArray *viewArray = [NSMutableArray arrayWithArray:self.navigationController.childViewControllers];
[viewArray removeObjectsInRange:NSMakeRange(findex, viewArray.count - 1)];
}
}
viewArray是新的childViewControllers。如果你有新的需求,可以编辑这个函数然后使用它。