ios popTOViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5753256/
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-30 19:40:24  来源:igfitidea点击:

popTOViewController

iosobjective-c

提问by Sam007

I have a button named 'HOME'. In that button action I have the following code:

我有一个名为“HOME”的按钮。在该按钮操作中,我有以下代码:

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

When I click this button my app crashes.

当我单击此按钮时,我的应用程序崩溃了。

Changing the index from 1 to 2, then it pops the view perfectly.

将索引从 1 更改为 2,然后完美弹出视图。

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

My view sequence is Page1 --> Page2 --> Page3

我的视图顺序是 Page1 --> Page2 --> Page3

I want to go from Page3 to Page1 but the app crashes. From Page3 to Page2 it works fine.

我想从 Page3 转到 Page1,但应用程序崩溃了。从 Page3 到 Page2 工作正常。

回答by Gypsa

Try this.

尝试这个。

Where I have written SeeMyScoresViewController you should write your View Controller class on which you have to go.(eg. Class of Home)

在我写 SeeMyScoresViewController 的地方,你应该写你必须去的视图控制器类。(例如家庭类)

NSArray *viewControllers = [[self navigationController] viewControllers];
for( int i=0;i<[viewControllers count];i++){
    id obj=[viewControllers objectAtIndex:i];
    if([obj isKindOfClass:[SeeMyScoresViewController class]]){
        [[self navigationController] popToViewController:obj animated:YES];
        return;
    }
}

回答by rckoenes

If you want to go to the root viewcontroller (page1) just use:

如果您想转到根视图控制器(page1),只需使用:

    [self.navigationController popToRootViewControllerAnimated:YES];

Also the first item in an index is not item 1 but item 0:

此外,索引中的第一项不是第 1 项而是第 0 项:

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

This should bring you back to the first viewController, but it will them be easier to use the popToRootViewController method.

这应该会让你回到第一个 viewController,但使用 popToRootViewController 方法会更容易。

回答by Leszek Zarna

Often it is more important to do that from top of stack, so:

通常从堆栈顶部执行此操作更为重要,因此:

In UINavigationController subclass or category:

在 UINavigationController 子类或类别中:

- (void)popToLast:(Class)aClass
{
    for (int i=self.viewControllers.count-1; i>=0; i--)
    {
        UIViewController *vc = self.viewControllers[i];
        if ([vc isKindOfClass:aClass])
        {
            [self popToViewController:vc animated:YES];
            break;
        }
    }
}

and you call that

你称之为

popToLast:[SomeViewController class];

回答by isair

An up-to-date way of popping back to a specific controller is:

返回到特定控制器的最新方法是:

[self.navigationController.viewControllers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

    if ([obj isKindOfClass:[MyViewController class]]) {
        [self.navigationController popToViewController:obj animated:YES];
        *stop = YES;
    }
}];

MyViewController is the controller you want to pop back to.

MyViewController 是您要弹回的控制器。