0

I would like to display an image in response to a local notification event that occurs while the iPhone is locked. When the user swipes on the notification, I would like my app to go to the root view controller, via popToViewController:animated, and then push a view controller that displays the image. This works when I set animated = YES. When animated = NO, the view controller that displays the image doesn't respond when the user taps the back button. Any thoughts on why the image view controller's navigation controls don't work when I popToViewController without animation? Thanks in advance.

Here's the relevant code...

- (void) localNotificationHandler
{
#ifdef kAnimatePop
    animated = YES;    // This works
#else
    animated = NO;     // This doesn't work
#endif
    _showImage = YES;

    // Check if this view controller is not visible
    if (self.navigationController.visibleViewController != self) {
        // Check if there are view controllers on the stack
        if ([self.navigationController.viewControllers count] > 1) {
            // Make this view controller visible
            [self.navigationController popToViewController:self animated:animated];
        }
    }
}    


- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (_showImage) {
        _showImage = NO;
        // Show image in a view controller
        [self performSegueWithIdentifier:@"MainToImageSegue" sender:self];
    }
}
0x141E
  • 12,613
  • 2
  • 41
  • 54

2 Answers2

1

You don't set _showImage until after popToViewController has been invoked. If it's animated viewDidAppear won't be called until later, so it unexpectedly works. If it's not animated then viewDidAppear is called immediately, before _showImage has been set. Move _showImage = true; to before the nav controller stack manipulations.

David Berry
  • 40,941
  • 12
  • 84
  • 95
  • Thanks for the answer, but that's not causing the issue. I'll edit my code to avoid confusion. – 0x141E Apr 07 '14 at 22:43
0

I might do the check this way:

- (void) localNotificationHandler{
...

...
    if (self.navigationController.topViewController != self) {
        [self.navigationController popToRootViewControllerAnimated:NO];
    }
}    

I would also recommend pushing your image view controller programmatically with something like

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

If your image view controller's navigation buttons are not working, it is likely because it is sending those navigation messages to nil. aka, the imageViewControllers self.navigationController is equal to nil. I'm not sure how to fix this with storyboards, but if you present it programatically that problem should go away.

Nick
  • 2,361
  • 16
  • 27
  • The root VC can be the topViewController but not be visible, if a modal VC is being displayed. I'm not sure if there is a difference between manually pushing the VC and using the segue method. BTW, the navigationController is not nil. Thanks for the answer. – 0x141E Apr 08 '14 at 07:03