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. 数据库-SQLite简介

    SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库(轻型数据库). 由于资源占用少.性能良好和零管理成本,嵌入式数据库有了它的用武之地,像Android.iPhone都有内置的S ...

  2. Ripple(瑞波币)validator-keys-tool 配置验证器

    目录 Ripple(瑞波币)validator-keys-tool配置验证器 验证器密钥工具指南 验证器密钥 验证器令牌(Validator Keys) public_key撤销 签名 Ripple( ...

  3. js 3行代码,最简易实现div效果悬浮

    简易实现浮动效果的首要因素是:获取滚动条距离浏览器顶部的距离,下面直接贴代码: <!DOCTYPE html> <html> <head> <meta cha ...

  4. 微信小程序开发,服务器端获取不到请求参数

    微信的request请求请求方式为 GET 时,可以在后台获取到请求的参数 userName 当为POST请求时则获取不到 userName 参数 解决方案是 在 设置一下 headers 里添加 ' ...

  5. 跟大佬一起读源码:CurrentHashMap的扩容机制

    并发编程——ConcurrentHashMap#transfer() 扩容逐行分析 前言 ConcurrentHashMap 是并发中的重中之重,也是最常用的数据结构,之前的文章中,我们介绍了 put ...

  6. 浅析人脸检测之Haar分类器方法:Haar特征、积分图、 AdaBoost 、级联

    浅析人脸检测之Haar分类器方法 一.Haar分类器的前世今生 人脸检测属于计算机视觉的范畴,早期人们的主要研究方向是人脸识别,即根据人脸来识别人物的身份,后来在复杂背景下的人脸检测需求越来越大,人脸 ...

  7. javascript加上标签

    在javascript脚本中可以加上标签,方便javascript程序进行快速定位,标签一般由一个合法的字符名称加上一个冒号组成,标签可以放在任意行的位置,这样可以为该行设置一个标记,然后再 结构体中 ...

  8. 【JavaScript 从零开始】 数字 文本 包装对象

    JavaScript中的算术运算 JavaScript 还自称更加复杂的算术运算,这些复杂的运算通过作为Math对象的属性定义的函数和常量来实现: Math.pow(2,53) //=>9007 ...

  9. linux命令新建文件

    在命令行输入vi filename就创建了一个叫filename的文件了,如果存在就打开了. 进入vi以后,输入内容,最后按一下esc,再按冒号,输入wq就保存退出了. 新建一个文本文件 vi New ...

  10. 转载:sql用逗号连接多张表对应哪个join?

    http://blog.csdn.net/huanghanqian/article/details/52847835 四种join的区别已老生常谈: INNER JOIN(也可简写为JOIN): 如果 ...