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. jQuery Event.which 属性

    which属性用于返回触发当前事件时按下的键盘按键或鼠标按钮. 对于键盘和鼠标事件,该属性用于确定你按下的是哪一个键盘按键或鼠标按钮. which属性对DOM原生的event.keyCode和even ...

  2. linux下清空c++ cin无效流的方式

    cin.clear(); cin.ignore(10000,'\n');//这里面的参数很重要

  3. 使用signtool.exe来验证程序的数字签名是否成功(命令行)

    signtool.exe是微软提供的数字签名工具,使用如下 signtool verify /pa YourApplicationName

  4. LeetCode【第1题】Two Sum

    准备刷一刷LeetCode了. 题目: ''' Given an array of integers, return indices of the two numbers such that they ...

  5. Classpath entry org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER will not be exported or published

    sometimes when importing a maven project in eclipse, i get the following error: Classpath entry org. ...

  6. 推荐的bootstrap之 formgroup表单布局样式

    一直没能找到比较好的Form Group样式,直到找到如下样式 转自 https://www.cnblogs.com/jokerjason/p/5721349.html <form class= ...

  7. 简单Tomcat HTTP RPC框架

    RPC基础知识 什么是RPC? RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. ...

  8. TCP/IP协议栈概述及各层包头分析

    TCP/IP协议栈中各层包头的分析 Protocol列表示的是该数据包最高层对应的协议,Length列表示该包的长度(包括从底层的协议到最高层的协议,其中包头一般是,链路层14字节,IP20字节,TC ...

  9. 关于打开fiddler后电脑无法正常上网的解决办法(fiddler抓包时的设置)

    关于fiddler如何抓取  ie内容(其中:360浏览器和qq浏览器均使用ie内核) 1.打开fiddler,进入tools-options,设置如下.这样配置后,打开fiddler,fiddler ...

  10. SpringMVC 之 Hello World 入门

    1 准备开发环境和运行环境 依赖 jar 包下载,如下图所示: 2 前端控制器的配置 在我们的web.xml中添加如下配置: <!-- The front controller of this ...