ios 如何从一个视图控制器弹出到另一个视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17859783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:30:20  来源:igfitidea点击:

How to pop from one view controller to another view controller

iphoneiosobjective-cuinavigationcontrolleruipopovercontroller

提问by Rahul Sharma

Using iOS I Have 15 ViewControllers now I want to pop from one ViewController to another View Controller.

使用 iOS 我现在有 15 个 ViewControllers 我想从一个 ViewController 弹出到另一个 ViewController。

I am using this code:

我正在使用此代码:

SecondViewController *Sec=[SecondViewController alloc]init];
[self.navigationController popViewController:Sec animated:YES];

This shows error this ViewController not existand then I am using this code:

这显示错误this ViewController not exist,然后我使用此代码:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];

This code is right to pop from thirdViewController to secondViewController. But What happened when we pop from Ninth(9th)ViewController to Fifth(5th)ViewController then I am using this code in Ninth(9th)ViewController:

这段代码适合从thirdViewController 弹出到secondViewController。但是当我们从 Ninth(9th)ViewController 弹出到 Fifth(5th)ViewController 然后我在 Ninth(9th)ViewController 中使用这段代码时发生了什么:

NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:4] animated:YES];

It does not pop from Ninth(9th)ViewController to Fifth(5th)ViewController apart that it pops Ninth(9th)ViewController to Eight(8th)ViewController. I don't know what happened when we use this line:

它不会从 Ninth(9th)ViewController 弹出到 Fifth(5th)ViewController,而是将 Ninth(9th)ViewController 弹出到第八 (8th)ViewController。我不知道当我们使用这条线时发生了什么:

NSArray *array = [self.navigationController viewControllers];
NsLog(@"array = %@",array);

When we use this in Ninth(9th)ViewController. NsLog shows:

当我们在Ninth(9th)ViewController. NsLog 显示:

array=   First(1st)ViewController;  
         Second(2nd)ViewController;
         Eight(8th)ViewController;
         Ninth(9th)ViewController;

I don't know why only Four View Controllers show. Whenever I am using 15 View Controllers. This problem occurs in each view controller. For instance if I am Using pop form fifteenth(15th)ViewController to Fifth(5th)ViewController then same problem manifests.

我不知道为什么只显示四个视图控制器。每当我使用 15 个视图控制器时。这个问题出现在每个视图控制器中。例如,如果我使用 pop 表单第十五(15)个视图控制器到第五(第 5 个)视图控制器,那么同样的问题就会出现。

NSArray *array = [self.navigationController viewControllers];
NsLog(@"array = %@",array);

array=     First(1st)ViewController;  
           Second(2nd)ViewController;
           fourteenth(14th)ViewController;
           fifteenth(15th)ViewController;

I want to count Number of ViewControllers and then pop to specific ViewController.

我想计算 ViewControllers 的数量,然后弹出到特定的 ViewController。

采纳答案by Pranit

Swift 4.0 - Swift 5.0

斯威夫特 4.0 - 斯威夫特 5.0

 for controller in self.navigationController!.viewControllers as Array {
            if controller.isKind(of: HomeViewController.self) {
                self.navigationController!.popToViewController(controller, animated: true)
                break
            }
        }

回答by Eric Genet

You can't pop to a new view controller (like you do with your secondViewController example).

你不能弹出一个新的视图控制器(就像你在 secondViewController 示例中所做的那样)。

When using a UINavigationController you

使用 UINavigationController 时,您

Add Controllerto the stack with:

使用以下命令将控制器添加到堆栈中:

[self.navigationController pushViewController:<yournewViewController> animated:YES];

Pop to the previous onewith :

弹出到上一个

[self.navigationController popViewControllerAnimated:YES];

Pop to a previous controllerin the stack (Must have been pushed before) :

弹出到堆栈中的前一个控制器(之前必须被推入):

[self.navigationController popToViewController:<ViewControllerToPopTo> animated:YES];

Go back to the rootController with

返回根控制器

[self.navigationController popToRootViewControllerAnimated:YES];

回答by KDeogharkar

for (UIViewController *controller in self.navigationController.viewControllers)
? ? ? ? {
? ? ? ? ? ? if ([controller isKindOfClass:[nameOfYourViewControllerYouWantToNavigate class]])
? ? ? ? ? ? {
? ? ? ? ? ? ? ? [self.navigationController popToViewController:controller animated:YES];

? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }

回答by vardhanReddy

Try this

尝试这个

 [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];

回答by Elto

First:

第一的:

 SecondViewController *Sec=[SecondViewController alloc]init];
 [self.navigationController popViewController:Sec animated:YES];

You can't do this because you allocate a new Secview controller that's not in a navigation controller.

您不能这样做,因为您分配了一个Sec不在导航控制器中的新视图控制器。

Consider using this:

考虑使用这个:

You are in 9 view controller

您在 9 视图控制器中

for (int i= 0 ; i < [[self.navigationController viewControllers]count] ; i++) {
    if ( [[[self.navigationController viewControllers] objectAtIndex:i] isKindOfClass:[FifiViewControllerClassname class]]) {
        [self.navigationController popToViewController:[array objectAtIndex:i] animated:YES];
    }
}

回答by iTag

Try like this

像这样尝试

MyTableViewController *vc = [[MyTableViewController alloc] init];
NSMutableArray *controllers = [NSMutableArray    
arrayWithArray:self.navigationController.viewControllers];
[controllers removeLastObject];
[controllers addObject:vc]; 

回答by Tejinder

BOOL check = FALSE;
NSArray *viewControllers = [[self navigationController] viewControllers];
id obj;
for( int i=0;i<[viewControllers count];i++)
{
    obj=[viewControllers objectAtIndex:i];
    if([obj isKindOfClass:[yourclassname class]])
    {
        check = TRUE;
        break;
    }
}

if (check)
{

    [[self navigationController] popToViewController:obj animated:YES];
}
else
{
    yourclassname *yourclassnameObj=[self.storyboard instantiateViewControllerWithIdentifier:@"yourclassStoryBoardID"];
    [self.navigationController pushViewController:yourclassnameObj animated:true];

}

回答by Paula Hasstenteufel

For Swift 3.0, use filter:

对于 Swift 3.0,使用过滤器:

let desiredViewController = self.navigationController!.viewControllers.filter { ##代码## is YourViewController }.first!
self.navigationController!.popToViewController(desiredViewController, animated: true)