I am using a UINavigationController
with a different Controller. I have overridden the BackButton
from the Navigation. In some cases I want to jump to RootController
if BackButton
is tapped.
But in this case the user jumps Back, but the whole View is empty. The behaviour is strange and I don’t know why. Can someone help me please.
To go to RootController
I do the following.
if([self.tempnavigationInfo.refkey isEqual:@"main"]){
[self.navigationController popToRootViewControllerAnimated:NO];
}
For the BackButton
I have implemented this handler:
@implementation UINavigationController (ShouldPopOnBackButton)
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
if([self.viewControllers count] < [navigationBar.items count]) {
return YES;
}
BOOL shouldPop = YES;
UIViewController* vc = [self topViewController];
if([vc respondsToSelector:@selector(navigationShouldPopOnBackButton)]) {
shouldPop = [vc navigationShouldPopOnBackButton];
}
if(shouldPop) {
dispatch_async(dispatch_get_main_queue(), ^{
[self popViewControllerAnimated:YES];
});
} else {
// Workaround for iOS7.1
for(UIView *subview in [navigationBar subviews]) {
if(subview.alpha < 1.) {
[UIView animateWithDuration:.25 animations:^{
subview.alpha = 1.;
}];
}
}
}
return NO;
}
In the ViewController I handle it this way:
-(BOOL) navigationShouldPopOnBackButton {
if (self.backConfirmation != nil
&& self.backConfirmation.navigation != nil
&& self.backConfirmation.confirmation == nil){
[[NSNotificationCenter defaultCenter] postNotificationName:@"gotoWithoutConfirmation" object:self.backConfirmation.navigation];
return NO;
}
return YES;
}
In my MainController I catch the notification and call the method with "popToRoot" logic above:
-(void) handleNotificationFromFormView:(NSNotification*) notification
{
if([notification.name isEqualToString:@"gotoWithoutConfirmation"]){
self.tempnavigationInfo = (NavigationInfo*)notification.object;
[self goBackTo];
}