ios 导航控制器 popViewControllerAnimated : yes 未按预期工作

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

Navigation controller popViewControllerAnimated : yes is not working as expected

iosuinavigationcontrollerpopviewcontrolleranimated

提问by user3441955

I am using following line of code:

我正在使用以下代码行:

[self.navigationController popViewControllerAnimated:YES];

But it is not behaving in ios 7 as it doing in ios 6.Some times it does not pop controller while we are pressing back button 2- 3 times in succession.
Resulting in abrupt behaviour in navigation bar and deallocating a controller but showing the same on ui .
So when we press anything on that controller it results to a crash since controller is already deallocated.

但它在 ios 7 中的行为不像在 ios 6 中那样。有时当我们连续按下后退按钮 2-3 次时,它不会弹出控制器。
导致导航栏中出现突然行为并取消分配控制器,但在 ui 上显示相同。
所以当我们在那个控制器上按下任何东西时,它会导致崩溃,因为控制器已经被释放了。

回答by cynistersix

Check if you're running the code on the UI thread

检查您是否在 UI 线程上运行代码

回答by Sunny Shah

[self.navigationController popToRootViewControllerAnimated:YES];

This method will navigate to the root of your navigationController.

此方法将导航到您的 navigationController 的根目录。

You can check your viewController hierachy With following code.

您可以使用以下代码检查您的 viewController 层次结构。

    NSLog(@"%@",self.navigationController.viewControllers);

回答by user5672256

I resolved this problem with this way:

我用这种方式解决了这个问题:

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                                                                     UINavigationController * nav = tabbarControl.selectedViewController;
                                                                     [nav.viewControllers objectAtIndex:0];
[nav setViewControllers:@[[nav.viewControllers objectAtIndex:0]] animated:NO];
                                                                tabbarControl.selectedIndex = 0;
 });

When you delay one second the view will pop from UI, then the view will pop from the navigation stack, I think is the problem of the animation serial.

当你延迟一秒视图会从UI弹出,然后视图会从导航堆栈弹出,我认为是动画序列的问题。

回答by David K. Lee

I think that should be working without dispatch_async. I got to the same issue, but i got to know the reason.

我认为这应该在没有 dispatch_async 的情况下工作。我遇到了同样的问题,但我知道原因。

We should check it if the current scene is assigned to a proper view controller name in the storyboard.(identity inspector -> class)

我们应该检查当前场景是否被分配给故事板中正确的视图控制器名称。(身份检查器 -> 类)

If you connect a button action to m file and then insert the name of the view controller, that is not working.

如果将按钮操作连接到 m 文件,然后插入视图控制器的名称,则不起作用。

So, you should delete the connect, and you insert the proper view controller name, and then you should connect the action to m file again.

因此,您应该删除连接,并插入正确的视图控制器名称,然后您应该再次将操作连接到 m 文件。

回答by catanore

I created my project from master-detail template, that uses split view controller. In my case, removing the split view controller resolved this issue.

我从使用拆分视图控制器的主从模板创建了我的项目。就我而言,删除拆分视图控制器解决了这个问题。

回答by Dzamir

I had the same problem on iOS 8.

我在 iOS 8 上遇到了同样的问题。

I solved by subclassing UINavigationController and adding this code:

我通过继承 UINavigationController 并添加以下代码来解决:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    self.delegate = self;
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    return [super popViewControllerAnimated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
{
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
}

I basically block all the user interactions during the pop animation. I know it's a dirty solution, but it's the only one that I found that solves the problem.

我基本上阻止了流行动画期间的所有用户交互。我知道这是一个肮脏的解决方案,但它是我发现的唯一解决问题的方法。

回答by Macrosoft-Dev

Try this code for popup a view controller from navigation stack

尝试使用此代码从导航堆栈中弹出视图控制器

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