IOS changes the system navigation bar's own return action event

Source: Internet
Author: User
Tags uikit

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.