benathon
benathon

Reputation: 7633

pushViewController followed by 'back' button sometimes doesn't pop view

I am using pushViewController to push a view in my application. Pressing the back button works about 95% of the time like you would expect. But if I go in and out of the view as fast as possible I run into a condition where the top bar moves as if a pop has occurred, but the view says. In this state, I am left with a back button, (in normal operation I have changed the text of this button to 'cancel'). pressing back will animate the top bar again, and then I am left with no buttons in the top bar, and I'm stuck inside the view.

Do you have any idea what might be going on here? Here are some more details:

The sub view calls these once or twice:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];

also the sub view is extending a BaseViewController. Inside this base controller all of the view methods are overloaded (they just call super). The one that might be interesting is:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self customizeNavigationBar];
}

- (void)customizeNavigationBar
{
    [self.navigationController.navigationBar setTintColor:UIColorFromRGB(kNavigationBackgroundColor)];
    UIBarButtonItem *backButton_ = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"ID_BUTTON_BACK", @"") style:UIBarButtonItemStyleBordered target:self action:nil];
    self.navigationItem.backBarButtonItem = backButton_;
    [backButton_ release];

}

Please let me know if you need more code or if I can explain things better.

--- Edit ---- I also am calling Google Analytics in view will appear. I remember this causing other issues in my app:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    NSError *error;
    if (![[GANTracker sharedTracker] trackPageview:@"/app_new_page"
                  withError:&error]) {    }
}

This code is being put in my actual view (not BaseViewController).

Upvotes: 0

Views: 1520

Answers (1)

benathon
benathon

Reputation: 7633

I found the problem. The issue was that I was calling setNavigationBarHidden:NO with animated:NO in viewDidLoad to show the nav bar without animation, but using pushViewContoller with animated:YES.

----- originally -----

[self.navigationController pushViewController:controller animated:YES];

and

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

The solution was to remove setNavigationBarHidden from viewDidLoad and move it into viewWillAppear, and to animate it the same way the view was animated. Since my nav bar was appearing instantly, it was possible to press back before the view controller had finished animating (pushing onto the stack), causing all these issues.

----- solution -----

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

Thanks for your help guys!

Upvotes: 1

Related Questions