user3441955
user3441955

Reputation: 161

Navigation controller popViewControllerAnimated : yes is not working as expected

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.

Upvotes: 16

Views: 30436

Answers (8)

Robin Daugherty
Robin Daugherty

Reputation: 7524

It is important that that calls to popViewController(animated:), popToRootViewController(animated:) and related calls be made in the main queue, but under some conditions this doesn't seem to be good enough, and the animation doesn't occur.

I was able to fix it as described in some other answers here, performing the pop navigation later in the main queue. The Swift equivalent:

DispatchQueue.main.async {
    self.rootViewController.popViewController(animated: true)
}

This might be explained by other animations that are still in progress, and by scheduling the block this way it happens at the end of the current work taking place or currently scheduled in the main queue, which allows the animation to execute correctly.

Upvotes: 0

Martin
Martin

Reputation: 4765

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

Upvotes: 0

David K. Lee
David K. Lee

Reputation: 131

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

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.

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.

Upvotes: 0

user5672256
user5672256

Reputation: 51

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.

Upvotes: 5

Dzamir
Dzamir

Reputation: 145

I had the same problem on iOS 8.

I solved by subclassing UINavigationController and adding this code:

- (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.

Upvotes: 0

cynistersix
cynistersix

Reputation: 1215

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

Upvotes: 10

Sunny Shah
Sunny Shah

Reputation: 13020

[self.navigationController popToRootViewControllerAnimated:YES];

This method will navigate to the root of your navigationController.

You can check your viewController hierachy With following code.

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

Upvotes: 7

Macrosoft-Dev
Macrosoft-Dev

Reputation: 2185

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];

Upvotes: -1

Related Questions