UINavigationController便于pop的category

效果图:

这个category是为了方便UINavigationController用于跳转到指定的控制器当中,用于跳级,如果pop的控制器不存在,会直接提示:

category源码:

UINavigationController+POP.h 与 UINavigationController+POP.m

//
// UINavigationController+POP.h
// YouXianMing
//
// Created by YouXianMing on 14-9-20.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface UINavigationController (POP) - (NSArray *)popToViewControllerClass:(Class)viewControllerClass animated:(BOOL)animated; @end
//
// UINavigationController+POP.m
// YouXianMing
//
// Created by YouXianMing on 14-9-20.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "UINavigationController+POP.h" @implementation UINavigationController (POP) - (NSArray *)popToViewControllerClass:(Class)viewControllerClass animated:(BOOL)animated
{
UIViewController *controller = nil;

for (UIViewController *oneController in self.viewControllers) {

if ([oneController isMemberOfClass:viewControllerClass]) {

controller = oneController;

break;

}

}

if (controller == nil) {
NSLog(@"%s:%s:%d 要pop的控制器指针为空", __FILE__, __func__, __LINE__);
return nil;
} return [self popToViewController:controller
animated:animated];
} @end

源码:

RootViewController.m

//
// RootViewController.m
// YouXianMing
//
// Created by YouXianMing on 14-9-20.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "RootViewController.h"
#import "SecondViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 设置导航栏样式以及标题以及view的背景色
[self titleTextAttributes:[self createTitleTextAttributes:[UIColor redColor]]];
self.title = @"First ViewController";
self.view.backgroundColor = [UIColor whiteColor]; // 添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapEvent)];
[self.view addGestureRecognizer:tap];
} - (void)tapEvent
{
[self.navigationController pushViewController:[SecondViewController new]
animated:YES];
} - (NCTitleAttribute *)createTitleTextAttributes:(UIColor *)color
{
NCTitleAttribute *titleAttribute = [NCTitleAttribute new];
titleAttribute.titleFont = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
titleAttribute.titleColor = color; return titleAttribute;
} @end

SecondViewController.m

//
// SecondViewController.m
// YouXianMing
//
// Created by YouXianMing on 14-9-20.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "SecondViewController.h"
#import "ThirdViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 设置导航栏样式以及标题
[self titleTextAttributes:[self createTitleTextAttributes:[UIColor redColor]]];
self.title = @"Second ViewController";
self.view.backgroundColor = [UIColor redColor]; // 添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapEvent)];
[self.view addGestureRecognizer:tap];
} - (void)tapEvent
{
[self.navigationController pushViewController:[ThirdViewController new]
animated:YES];
} - (NCTitleAttribute *)createTitleTextAttributes:(UIColor *)color
{
NCTitleAttribute *titleAttribute = [NCTitleAttribute new];
titleAttribute.titleFont = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
titleAttribute.titleColor = color; return titleAttribute;
} @end

ThirdViewController.m

//
// ThirdViewController.m
// YouXianMing
//
// Created by YouXianMing on 14-9-20.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ThirdViewController.h"
#import "RootViewController.h" @interface ThirdViewController () @end @implementation ThirdViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 设置导航栏样式以及标题
[self titleTextAttributes:[self createTitleTextAttributes:[UIColor redColor]]];
self.title = @"Third ViewController";
self.view.backgroundColor = [UIColor cyanColor]; // 添加手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapEvent)];
[self.view addGestureRecognizer:tap];
} - (void)tapEvent
{
[self.navigationController popToViewControllerClass:[RootViewController class]
animated:YES];
} - (NCTitleAttribute *)createTitleTextAttributes:(UIColor *)color
{
NCTitleAttribute *titleAttribute = [NCTitleAttribute new];
titleAttribute.titleFont = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
titleAttribute.titleColor = color; return titleAttribute;
} @end

需要注意的一些地方:

UINavigationController便于pop的category的更多相关文章

  1. iOS 7中使用UINavigationController进行pop崩溃

    最近在一个项目中遇到一种情况,push到一个界面,如果那个界面未请求到数据,则直接pop回去,然而使用 [self.navigationController popViewControllerAnim ...

  2. iOS 开发中遇到的问题

    1. 关于纠结很久的KVO崩溃问题,其真正原因是,在删除roomItem的KVO之前,将这个对象已经赋值为nil,所以实际上并没有删除他的observer,因此而崩溃:长时间纠结的原因是受.cxx_d ...

  3. [New learn] 设计模式

    本文翻译自:http://www.raywenderlich.com/46988/ios-design-patterns iOS设计模式 - 你可能听到过这个术语,但是你知道是什么意思吗?虽然大多数的 ...

  4. Log4cpp介绍及使用

    Log4cpp是一个开源的C++类库,它提供了在C++程序中使用日志和跟踪调试的功能.使用log4cpp,可以很便利地将日志或者跟踪调试信息写入字符流.内存字符串队列.文件.回滚文件.调试器.Wind ...

  5. log4cpp基础测试

    // log4cplus.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" #include <iostream>#include ...

  6. JVM Specification 9th Edition (4) Chapter 4. The class File Format

    Chapter 4. The class File Format Table of Contents 4.1. The ClassFile Structure 4.2. Names 4.2.1. Bi ...

  7. 精准 iOS 内存泄露检测工具

    MLeaksFinder:精准 iOS 内存泄露检测工具 发表于 2016-02-22   |   zepo   |   23 Comments 背景 平常我们都会用 Instrument 的 Lea ...

  8. iOS页面右滑返回的实现方法总结

    1.边缘触发的系统方法 ①系统返回按钮 self.navigationController.interactivePopGestureRecognizer.enabled = YES;  ②自定义返回 ...

  9. UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解

    (1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最下面,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...

随机推荐

  1. 机器学习--聚类系列--DBSCAN算法

    DBSCAN算法 基本概念:(Density-Based Spatial Clustering of Applications with Noise) 核心对象:若某个点的密度达到算法设定的阈值则其为 ...

  2. caffe, caffe2, paddlepaddle, tensorflow对于cuda,cudnn,protobuf依赖的纠葛

    由于在学习神经网络,为了尝试各种深度学习框架,电脑上目前安装了caffe, caffe2, paddlepaddle, tensorflow三款主流框架,但是安装过程中真是痛不欲生. 且不说单单安装一 ...

  3. Java并发编程笔记之LongAdder和LongAccumulator源码探究

    一.LongAdder原理 LongAdder类是JDK1.8新增的一个原子性操作类.AtomicLong通过CAS算法提供了非阻塞的原子性操作,相比受用阻塞算法的同步器来说性能已经很好了,但是JDK ...

  4. iOS 微信支付SDK与微信友盟分享两者同时集成时,出现的问题与解决之路。

    这两天改版一个旧的APP,要旧貌换新颜,拿到app后进行编译,一直报下面的错误. 报不认识的符号名PayReq错误.奇怪,啥也没动就这样,真不知道给的包是不是本来就是个报错的工程. 不管怎样,要对它修 ...

  5. C# using用法

    一.using指令 使用using指令在文件顶部引入命名空间,如 using System; using System.IO; 二.using别名 用using为命名空间或类型定义别名,当引入的多个命 ...

  6. [转]ASP.NET MVC4中@model使用多个类型实例的方法

    本文转自:http://blog.csdn.net/hulihui/article/details/48199897 有时需要在ASP.NET MVC4的视图的@model中使用多个类型的实例,.NE ...

  7. jquery 关于使用 append 追加 元素后 事件无法触发

    当在使用js或jQuery创建元素时,用 on(事件,function(){代码}) 或者 事件(function(){代码 })绑定事件时 在使用append添加元素后 由于是在页面加载完成之后进行 ...

  8. 钉钉微应用接入钉钉免登陆配置记录。NET实现

    在这里记录一下我配置的钉钉接入微应用遇到的坑.搞了我几天天才调通.头皮发麻,现在梳理一下,以免别人也入坑. 1.钉钉接入主要要获取钉钉企业员工的ID,然后去自己的应用的数据库里进行匹配然后实现免登陆的 ...

  9. 线程7--GCD的基本使用

    子线程执行延时操作,执行完成后返回主线程更新界面 dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DE ...

  10. 啰里吧嗦redis

    1.redis是什么 redis官网地址 Redis is an open source (BSD licensed), in-memory data structure store, used as ...