Cat Learning iOS Weibo project Combat (6) sliding fallback function of navigation controller Navigationcontroller

Source: Internet
Author: User

Cat Share, must boutique

Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243

One: Effect

In the second chapter, I wrote how to customize the navigation to implement a custom navigation controller left and right button style, but when we implement it ourselves, the system comes back to you with the ability to swipe to a backward function can not be used.

The main implementation of sliding fallback function here

Two: Code implementation ideas

First put a popdelegate in nynavigationcontroller.m to place the gesture proxy object you want to change

@interface NYNavigationController ()<UINavigationControllerDelegate>@property (nonatomicstrongid popDelegate;@end

Ways to override Uinavigationcontrollerdelegate- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated

The proxy method is set in Viewdidload, and the gesture proxy is preset to restore

- (void)viewDidLoad {    [super viewDidLoad];    //记住手势代理 用来还原    self.interactivePopGestureRecognizer.delegate;    self.delegateself;}
//Navigation controller jump complete Controller- (void) Navigationcontroller: (Uinavigationcontroller*) Navigationcontroller Didshowviewcontroller: (Uiviewcontroller*) Viewcontroller Animated: (BOOL) animated{if(Viewcontroller = = Self. Viewcontrollers[0]) {//is the root controller        //Reset gesture Agent         Self. Interactivepopgesturerecognizer. Delegate= _popdelegate; }Else{//non-root controller        //Set gesture agent to be empty, you can implement sliding        //Implement sliding return function        //Clear the swipe back gesture of the agent, you can realize the sliding back function.          Self. Interactivepopgesturerecognizer. Delegate=Nil; }}
Three: all the Navigationcontroller code

Internal includes setting left and right buttons and so on functions

////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 () <uinavigationcontrollerdelegate>@property(nonatomic,Strong)IDPopdelegate;@end @implementation nynavigationcontroller + (void) initialize{//Get the Uibarbuttonitem below the current class    Uibarbuttonitem*item = [UibarbuttonitemAppearancewhencontainedin: Self,Nil];//Set the text color of the navigation bar button to yellow    nsmutabledictionary*titleattr = [nsmutabledictionaryDictionary]; Titleattr[nsforegroundcolorattributename] = [UicolorOrangecolor]; [item settitletextattributes:titleattr forstate:uicontrolstatenormal];} - (void) Viewdidload {[SuperViewdidload];//Remember gesture proxy is used to restore_popdelegate = Self. Interactivepopgesturerecognizer. Delegate; Self. Delegate= Self;} -(void) Pushviewcontroller: (Uiviewcontroller*) Viewcontroller Animated: (BOOL) animated{[SuperPushviewcontroller:viewcontroller animated:animated];//Set non-root controller navigation bar contents    if( Self. Viewcontrollers. Count!=0) {//non-root controller        //Set the contents of the navigation bar        //Set navigation bar left and right        //If the Back button on the navigation bar is overwritten, there is no sliding back function        //Set left buttonViewcontroller. Navigationitem. Leftbarbuttonitem= [Uibarbuttonitembarbuttonitemwithimage:[UIImageimagenamed:@"Navigationbar_back"] Highimage:[UIImageimagenamed:@"navigationbar_back_highlighted"] Target: SelfAction@selector(Backtopre) forcontrolevents:uicontroleventtouchupinside];//Set right buttonViewcontroller. Navigationitem. Rightbarbuttonitem= [Uibarbuttonitembarbuttonitemwithimage:[UIImageimagenamed:@"Navigationbar_more"] Highimage:[UIImageimagenamed:@"navigationbar_more_highlighted"] Target: SelfAction@selector(Backtoroot) forcontrolevents:uicontroleventtouchupinside]; }}-(void) backtopre{//Return to previous controller[ SelfPopviewcontrolleranimated:YES];} -(void) backtoroot{//return root controller[ SelfPoptorootviewcontrolleranimated:YES];}#pragma mark-uinavigationcontrollerdelegate for sliding fallback function//Navigation controller jump complete Controller- (void) Navigationcontroller: (Uinavigationcontroller*) Navigationcontroller Didshowviewcontroller: (Uiviewcontroller*) Viewcontroller Animated: (BOOL) animated{if(Viewcontroller = = Self. Viewcontrollers[0]) {//is the root controller        //Reset gesture Agent         Self. Interactivepopgesturerecognizer. Delegate= _popdelegate; }Else{//non-root controller        //Set gesture agent to be empty, you can implement sliding        //Implement sliding return function        //Clear the swipe back gesture of the agent, you can realize the sliding back function.          Self. Interactivepopgesturerecognizer. Delegate=Nil; }}@end
Four: note

Set the gesture agent to be empty after the use of the time to set back, the system inside things can not randomly change, or there will be unpredictable bugs. With the controller, accidentally did a fallback slide action will let the navigation controller again into the next page of the right button click Invalid, the app crashes.

self.interactivePopGestureRecognizer.delegatenil;

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cat Learning iOS Weibo project Combat (6) sliding fallback function of navigation controller Navigationcontroller

Related Article

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.