Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing navigationController.delegate causes to bad access

This may sound a newbie question, however I'm new t iOS dev.

I've a view pushed in navigationController, let say it is the 3rd pushed view. In that view I set self.navigationController.delegate = self;. I've changed delegate because I need to handle case when user goes to previous view i.e. pops from current view.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([[viewController class] isEqual:[MainViewController class]]) {
        ...
    }
}

It works OK, but when I pop the current view and press navigation back button again (i.e. switching to first pushed view) I'm getting bad access error.

So what I'm missing ? What is the correct way to handle navigation back button press ?

like image 690
deimus Avatar asked Nov 05 '11 08:11

deimus


1 Answers

It's because navigation controller sends a message to popped and deallocated view controller, you have to set the delegate each time you do the popping and pushing. Also add self.navigationController.delegate = nil; to dealloc method of your viewController.

like image 64
Eugene Avatar answered Oct 22 '22 11:10

Eugene