UINavigation controller array bug
I was just wondering about a bug I came across when popping my UINavigationController. Whatever UIViewController I am currently in, I have implemented a logout controller that pops back to the root c开发者_如何转开发ontroller in my UINavigationController. The main code that does this is listed here.
NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);
This is what the console displays.
Root Controller: DetailViewController
Root Controller: (null)
However when I change the above code to:
NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
NSLog(@"Root Controller: %@", [[self.navigationController.viewController objectAtIndex:0] class]);
The results in the console is displayed as:
Root Controller: DetailViewController
Root Controller: DetailViewController
It was bothering me for a while why when I popped the UINavigationController to the DetailViewController that it didnt fire the ViewWillAppear method to update my login information. It seems to me that when I pop to the 0 index it is accessing an array object that is out of bounds but when I pop to index 1, thats where the current root controller is. Does anyone know what the cause of this would be?
My guess is that self.navigationController
is being set to nil after self
is popped off the stack.
If that's the case, it looks like popping to the view controller at index 1 does not reset self.navigationController
to nil (either because self
is the view controller at index 1 or some call in viewWill|Did?Appear is setting it again).
精彩评论