Tmall learn iOS Weibo Project Practice (6) Implementation of the navigation controller NavigationController's slide rollback function, ios Sina Weibo project source code
CAT/CAT sharing, must be excellent
For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Address: http://blog.csdn.net/u013357243
I. Effect
The second article describes how to customize the navigation controller's left and right button styles, but when we implement it ourselves, the system's built-in slide to the right to implement the rollback function is useless.
Here we mainly implement the sliding rollback function
.
Ii. Code Implementation
First, put a popDelegate in NYNavigationController. m to place the gesture proxy object to be changed.
@interface NYNavigationController ()<UINavigationControllerDelegate>@property (nonatomic, strong) id popDelegate;@end
Override UINavigationControllerDelegate- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
Sets the proxy method in viewDidLoad, and sets the gesture proxy to restore
-(Void) viewDidLoad {[super viewDidLoad]; // remember the gesture proxy to restore _ popDelegate = self. interactivepopgsturerecognizer. delegate; self. delegate = self ;}
// Navigation controller-(void) navigationController :( UINavigationController *) navigationController didShowViewController :( UIViewController *) viewController animated :( BOOL) animated {if (viewController = self. viewControllers [0]) {// root controller // restore gesture proxy self. interactivepopgsturerecognizer. delegate = _ popDelegate;} else {// non-root controller // set the gesture proxy to null, you can move the slider to implement the sliding return function // clear the proxy of the sliding return gesture to implement the sliding return function. Self. interactivepopgsturerecognizer. delegate = nil ;}}
III. All navigationController code
Internal functions include setting left and right buttons, etc.
/// NYNavigationController. m // cat Weibo /// Created by apple on 15-7-29. // Copyright (c) 2015 znycat. all rights reserved. // # import "NYNavigationController. h "# import" UIBarButtonItem + Item. h "@ interface NYNavigationController () <strong> @ property (nonatomic, strong) id popDelegate; @ end @ implementation NYNavigationController + (void) initialize {// obtain UIBarButtonItem UIBarButton under the current class Item * item = [UIBarButtonItem appearanceWhenContainedIn: self, nil]; // set the text color of the navigation bar button to yellow NSMutableDictionary * titleAttr = [NSMutableDictionary dictionary]; titleAttr [NSForegroundColorAttributeName] = [UIColor orangeColor]; [item setTitleTextAttributes: titleAttr forState: UIControlStateNormal];}-(void) viewDidLoad {[super viewDidLoad]; // remember that the gesture proxy is used to restore _ popDelegate = self. interactivepopgsturerecognize R. delegate; self. delegate = self;}-(void) pushViewController :( UIViewController *) viewController animated :( BOOL) animated {[super pushViewController: viewController animated: animated]; // set the non-root controller navigation bar content if (self. viewControllers. count! = 0) {// non-root controller // set the content of the navigation bar // set the left and right sides of the navigation bar // if the return buttons on the navigation bar are overwritten, then there is no sliding return function // set the left button viewController. navigationItem. leftBarButtonItem = [UIBarButtonItem failed: [UIImage imageNamed: @ "navigationbar_back"] highImage: [UIImage imageNamed: @ "success"] target: self action: @ selector (backToPre) forControlEvents: UIControlEventTouchUpInside]; // set the right button to viewController. navigationIte M. rightBarButtonItem = [UIBarButtonItem attributes: [UIImage imageNamed: @ "navigationbar_more"] highImage: [UIImage imageNamed: @ "Custom"] target: self action: @ selector (backToRoot) forControlEvents: UIControlEventTouchUpInside];}-(void) backToPre {// returns the previous controller [self popViewControllerAnimated: YES];}-(void) backToRoot {// returns the root controller [self popToRootViewControllerAnimated: YES];} # Pragma mark-pilot implement the slide rollback function // the Controller that completes the navigation controller jump-(void) navigationController :( UINavigationController *) navigationController didShowViewController :( UIViewController *) viewController animated :( BOOL) animated {if (viewController = self. viewControllers [0]) {// root controller // restore gesture proxy self. interactivepopgsturerecognizer. delegate = _ popDelegate;} else {// non-root controller // set the gesture proxy to null to achieve sliding The return function // clears the proxy of the sliding return gesture to implement the sliding return function. Self. interactivepopgsturerecognizer. delegate = nil; }}@ end
4. Note:
After the gesture proxy is set to null, you must set it back when it is used. Internal tasks in the system cannot be changed randomly or unexpected bugs may occur. When you perform the rollback and slide operation with the Controller, the app will crash if you click the button on the right of the navigation controller that enters the next page again.
self.interactivePopGestureRecognizer.delegate = nil;
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.