#import <UIKit/UIKit.h>
@protocol Backbuttonhandlerprotocol <NSObject>
@optional
Override This method in Uiviewcontroller derived class to handle ' Back ' button click
-(BOOL) Navigationshouldpoponbackbutton;
@end
@interface Uiviewcontroller (Backbuttonhandler) <BackButtonHandlerProtocol>
@end
#import "Uiviewcontroller+backbuttonhandler.h"
@implementation Uiviewcontroller (Backbuttonhandler)
@end
@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. Thanks to @boliva-http://stackoverflow.com/posts/comments/34452906
For (UIView *subview in [Navigationbar Subviews]) {
if (Subview.alpha < 1.) {
[UIView animatewithduration:.25 animations:^{
Subview.alpha = 1.;
}];
}
}
}
return NO;
}
@end
#import "ViewController.h"
#import "Uiviewcontroller+backbuttonhandler.h"
@implementation Viewcontroller
-(void) viewdidload
{
[Super Viewdidload];
Self.title = [NSString stringwithformat:@ "screen-%d", Self.navigationController.viewControllers.count];
Self.navigationItem.rightBarButtonItem = [[Uibarbuttonitem alloc] initwithtitle:@ "Next" style: Uibarbuttonitemstyleplain
Target:selfaction: @selector (onnextbtn:)];
}
-(void) ONNEXTBTN: (ID) sender
{
[Self.navigationcontroller Pushviewcontroller:[viewcontroller new] animated:yes];
}
-(BOOL) Navigationshouldpoponbackbutton///write the event handling of the return button in this method
{
[Self.navigationcontroller Popviewcontrolleranimated:yes];
return YES;
}
IOS changes the system navigation bar's own return action event