如何从导航控制器ios中删除以前的视图控制器?
我想从导航堆栈中删除以前的视图控制器 比如说 ->A是根视图。现在导航到B-B导航到c-c导航到c-c导航到c -->现在我想删除所有c视图控制器&弹出到B B不是固定视图控制器 另一个例子 A->b->c>g>c>f>c>c>c>c>c 从导航中删除所有c视图控制器&需要以下输出 A->b->c>g>c>f如何从导航控制器ios中删除以前的视图控制器?,ios,objective-c,Ios,Objective C,我想从导航堆栈中删除以前的视图控制器 比如说 ->A是根视图。现在导航到B-B导航到c-c导航到c-c导航到c -->现在我想删除所有c视图控制器&弹出到B B不是固定视图控制器 另一个例子 A->b->c>g>c>f>c>c>c>c>c 从导航中删除所有c视图控制器&需要以下输出 A->b->c>g>c>f 请帮助我您可以使用popToViewController() 例如: 在目标C中 [self.navigationController popToViewController:self.n
请帮助我您可以使用
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
斯威夫特3
self.navigationController.popToViewController( self.navigationController.viewControllers[your_VC_index], animated: true)
您可以使用
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
斯威夫特3
self.navigationController.popToViewController( self.navigationController.viewControllers[your_VC_index], animated: true)
从导航堆栈中识别控制器。然后跳到那个控制器
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;
}
}
从导航堆栈中识别控制器。然后跳到那个控制器
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;
}
}
反向迭代导航堆栈,获取第一个不匹配的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)
}
反向迭代导航堆栈,获取第一个不匹配的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)
}
写一个方法,如下所示
- (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;
}
写一个方法,如下所示
- (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;
}
因为navigationController的ChildViewController是一个堆栈,
因此,使用此函数,您可以弹出到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的新ChildViewController。您可以使用以下功能:
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是新的ChildViewController。如果您有新的需求,可以编辑此函数,然后使用它。因为navigationController的ChildViewController是一个堆栈,
因此,使用此函数,您可以弹出到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的新ChildViewController。您可以使用以下功能:
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是新的ChildViewController。如果您有新的需求,可以编辑此函数,然后使用它。这可能会对您有所帮助:我已经检查过了。但无法工作我不理解的是,您如何从c导航到c。>A->b->c>g>c>f>c>c>c这可能会对你有所帮助:我已经检查过了。但是没有工作。我不明白的是,你怎么能从c导航到cA->b->c>g>c>f>c>c>c>cit将通过在导航堆栈中传递上面的所有视图控制器,转到第一个不匹配的viewcontroller是的,这就是他想要的。。检查他的示例@suhit导航堆栈是'A->b->c>g>c>f>c>c>c'所需的输出是'A->b->c>g>c>f',您的解决方案将显示ViewController-A,因为它将在第一次索引时停止。@suhit感谢您提供的信息。。我刚刚更新了答案。通过在导航堆栈中传递上面的所有视图控制器,它将转到第一个不匹配的viewcontroller是的,这就是他想要的。。检查他的示例@suhit导航堆栈是'A->b->c>g>c>f>c>c>c'所需的输出是'A->b->c>g>c>f',您的解决方案将显示ViewController-A,因为它将在第一次索引时停止。@suhit感谢您提供的信息。。我刚刚更新了答案。