How can I change the view controller the default navigation back button takes me to? The back button usually takes you back to the previous view controller. But what if I wa
You can do this by adding a custom back button like this. Add your custom action handler on tap on Back button on navigation bar.
+ (void)addCustomBackButtonForController:(id )iViewController withNavBar:(UINavigationController *)iNavController andNavItem:(UINavigationItem *)iNavItem {
if ([self isIOS7orAbove]) {
UIImage *backArrow = [UIImage imageNamed:kMyIOS7BackButtonImage];
UIButton *aBackButton = [UIButton buttonWithType:UIButtonTypeSystem];
CGSize aBackButtonTextSize = [MyLocalizedBackButton sizeWithFont:[UIFont systemFontOfSize:17.0]];
aBackButton.frame = CGRectMake(0.0, 0.0, aBackButtonTextSize.width + backArrow.size.width, iNavController.navigationBar.frame.size.height);
aBackButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
SEL backSelector = NSSelectorFromString(@"backAction");
[aBackButton addTarget:iViewController action:backSelector forControlEvents:UIControlEventTouchUpInside];
[aBackButton setTitle:MyLocalizedBackButton forState:UIControlStateNormal];
[aBackButton setImage:backArrow forState:UIControlStateNormal];
[aBackButton setExclusiveTouch:YES];
if ([self isIOS7orAbove]) {
aBackButton.titleLabel.font = [UIFont systemFontOfSize:17.0];
} else {
aBackButton.titleLabel.font = [UIFont boldSystemFontOfSize:12.0];
}
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aBackButton];
UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = kMyIOS7BarButtonNegativeSpacerWidth;
iNavItem.leftBarButtonItems = @[aNegativeSpacer, aLeftBarButtonItem];
} else {
UIButton *aCustomBackButton = [UIButton buttonWithType:101];
SEL backSelector = NSSelectorFromString(@"backAction");
[aCustomBackButton addTarget:iViewController action:backSelector forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:MyLocalizedBackButton forState:UIControlStateNormal];
[aCustomBackButton setExclusiveTouch:YES];
if ([self isIOS7orAbove]) {
aCustomBackButton.titleLabel.font = [UIFont systemFontOfSize:kFontSize17];
} else {
aCustomBackButton.titleLabel.font = [UIFont boldSystemFontOfSize:kFontSize12];
}
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
iNavItem.leftBarButtonItem = aLeftBarButtonItem;
}
}