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. BFS和DFS详解以及java实现

    前言 图在算法世界中的重要地位是不言而喻的,曾经看到一篇Google的工程师写的一篇<Get that job at Google!>文章中说到面试官问的问题中几乎有一半的问题都可以用图的 ...

  2. C/C++中的常量到底存在了什么地方

    一般来说,基本类型(整型.字符型等)常量会在编译阶段被编译成立即数,占的是代码段的内存.(代码段是只读的,而且不允程序员获取代码段的地址,所以在c++中,尽量不为const分配数据段的内存,但是一旦取 ...

  3. 短视频APP是如何开启你的美好生活的?

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯视频云终端团队发表于云+社区专栏 常青, 2008 年毕业加入腾讯,一直从事客户端研发相关工作,先后参与过 PC QQ.手机QQ. ...

  4. js判断手机是否安装了某一款app,有则打开,没有去下载

    function openApp(){ if(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { var loadDateTime = new ...

  5. 通向全栈之路——(3)node环境搭建

    1:更新系统 sudo apt-get update2:安装相关软件 sudo apt-get install vim openssl build-essential libssl-dev wget ...

  6. 如何编写makefile文件

    最近一直在学习makefile是如何编写的.       当我们写的程序文件比较少的时候,敲入gcc /g++,当你在大型工程中,在一个个编译文件的话,你可能就会很郁闷.linux有一个自带的make ...

  7. jQuery如何根据元素值删除数组元素

    用到的方法$.inArry(); $.inArray( value, array [, fromIndex ] ) value 任意类型 用于查找的值. array Array类型 指定被查找的数组. ...

  8. Java学习--内置对象(其他的)

  9. iOS开发常用函数

    1. 三角函数 double sin (double); 正弦 double cos (double);余弦 double tan (double);正切 2 .反三角函数  double asin ...

  10. PHP · MySQL函数

    连接名=mysql_connect("主机","用户名","密码"); 连接名=mysql_qconnect("主机", ...