简体   繁体   中英

navigationController is NULL after pressing the back button

I have an app that has a Navigation Controller built from storyboard. Each view controller has a table that needs to be refreshed each time the user goes back from the next view, so let's say if the user goes like view controller A->B->C, then when he goes back to BI need to refresh the values on the table in view B.

I'm trying to do this using UINavigationControllerDelegate, so that didShowViewController is called each time the user goes back and forth in the navigation:

- (void) navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // Do some initialization stuff
    // and then get entries from an external database

   [self fetchEntries];

   // Log:
   NSLog(@"View-B: \nself=%@ \nnavigationController=%@\nDelegate:%@\nviewControllers:%@", self, self.navigationController, self.navigationController.delegate, self.navigationController.viewControllers);
}

I set the delegate on viewDidLoad:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // Sets the delegate for Navigation Controller
   [self.navigationController setDelegate:self];   
}

The problem is that from some reason navigationController is being set to NULL somewhere and I can't find where it is, since I'm not doing any explicit push/pop operation (as I mentioned the navigation controller was built in Storyboard).

The exact sequence is : A->B->C->B and here navigationController is NULL. Here are some NSLog outputs:

1) When user goes from A->B

2012-10-07 14:50:28.170 MyApp[35765:207] View-B: 
self=<View-B: 0x6a660b0> 
navigationController=<UINavigationController: 0x6823b20>
Delegate:<View-B: 0x6a660b0>
viewControllers:(
    "<View-A: 0x6879e90>",
    "<View-B: 0x6a660b0>"
)

2) B->C

2012-10-07 14:50:31.371 MyApp[35765:207] View-C: 
self=<View-C: 0x6a69a10> 
navigationController=<UINavigationController: 0x6823b20>
Delegate:<View-C: 0x6a69a10>
viewControllers:(
    "<View-A: 0x6879e90>",
    "<View-B: 0x6a660b0>",
    "<View-C: 0x6a69a10>"
)

3) And then C->B

2012-10-07 14:50:37.027 MyApp[35765:207] View-C: 
self=<View-C: 0x6a69a10> 
navigationController=(null)
Delegate:(null)
viewControllers:(null)

I really don't understand why navigationController goes to NULL here. I found that the problem happens when I set the delegate in viewDidLoad. If I remove that line and move the code from the protocol procedure (didShowViewController) to viewDidLoad everything works fine, except that I can't refresh my tables when the user goes back in the navigation stack.

Also weird is that in step (3) didShowViewController is being called from View-C and not from View-B.

Any help is much appreciated.

I believe that you should call [self.tableview reloadData]; at the viewWillAppear of viewController B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM