UINavigationController便于pop的category
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的更多相关文章
- iOS 7中使用UINavigationController进行pop崩溃
最近在一个项目中遇到一种情况,push到一个界面,如果那个界面未请求到数据,则直接pop回去,然而使用 [self.navigationController popViewControllerAnim ...
- iOS 开发中遇到的问题
1. 关于纠结很久的KVO崩溃问题,其真正原因是,在删除roomItem的KVO之前,将这个对象已经赋值为nil,所以实际上并没有删除他的observer,因此而崩溃:长时间纠结的原因是受.cxx_d ...
- [New learn] 设计模式
本文翻译自:http://www.raywenderlich.com/46988/ios-design-patterns iOS设计模式 - 你可能听到过这个术语,但是你知道是什么意思吗?虽然大多数的 ...
- Log4cpp介绍及使用
Log4cpp是一个开源的C++类库,它提供了在C++程序中使用日志和跟踪调试的功能.使用log4cpp,可以很便利地将日志或者跟踪调试信息写入字符流.内存字符串队列.文件.回滚文件.调试器.Wind ...
- log4cpp基础测试
// log4cplus.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h" #include <iostream>#include ...
- 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 ...
- 精准 iOS 内存泄露检测工具
MLeaksFinder:精准 iOS 内存泄露检测工具 发表于 2016-02-22 | zepo | 23 Comments 背景 平常我们都会用 Instrument 的 Lea ...
- iOS页面右滑返回的实现方法总结
1.边缘触发的系统方法 ①系统返回按钮 self.navigationController.interactivePopGestureRecognizer.enabled = YES; ②自定义返回 ...
- UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解
(1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最下面,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...
随机推荐
- linux 查找删除找定文件
find . -name "*.lastUpdated" -exec rm -rf {} \; 这个命令是find的基本用法,可以分两部分,find ~/ -name " ...
- H5如何用Canvas画布生成并保存带图片文字的新年快乐的海报
摘要:初略算了算大概有20天没有写博客了,原本是打算1月1号元旦那天写一个年终总结的,博客园里大佬们都在总结过去,迎接将来,看得我热血沸腾,想想自己也工作快2年了,去年都没有去总结一下,今年势必要总结 ...
- Dubbo2.7源码分析-SPI的应用
SPI简介 SPI是Service Provider Interface的缩写,即服务提供接口(翻译出来好绕口,还是不翻译的好),实质上是接口,作用是对外提供服务. SPI是Java的一种插件机制,可 ...
- 追踪MySQL中长时间运行的事务
接上一篇,未提交的事务导致锁等待,锁住一段时间后又提交了(也可能是回滚),锁释放业务继续运行.那么我们如何知道产生锁的事务是在跑什么SQL? 今天讲的这个工具,就可以通过监听3306端口的数据,把每个 ...
- autofac使用Common Serivce Locator跟随wcf,mvc,web api的实例控制
autofac本身只提供了基本的ioc容器的功能 要想在mvc,wcf,web api中使用,除了autofac本身,还需要引入对应的包(点击对应连接可查看文档) 除此之外,使用Common Serv ...
- C#中深拷贝和浅拷贝
一:概念 内存:用来存储程序信息的介质. 指针:指向一块内存区域,通过它可以访问该内存区域中储存的程序信息.(C#也是有指针的) 值类型:struct(整形.浮点型.decimal的内部实现都是str ...
- C# 数据库增加新表怎么更新ef
找到dbmx文件,打开,右键,更新模型 也可以 右键从模型中删除 重新更新过来,感觉说了句废话 哈哈
- C# 进程间共享内存通信方式
从别处看到一篇文章做进程间通信很好使,唯一的问题是,需要注意using的用法,Using有个用法3, using 语句允许程序员指定使用资源的对象应当何时释放资源.using 语句中使用的对象必须实现 ...
- 【基于初学者的SSH】struts2 值栈的详解与struts2标签库+ognl表达式
一:什么是值栈:struts2里面本身提供的一种存储机制,类似于域对象,值栈,可以存值和取值 特点:先进后出,最上面的元素叫做栈顶,也叫压栈. <s:debug></s:debug& ...
- ibatis中$和#的区别
比如当变量name的类型是Stirng时, $name$ 打印出来的是 张三 #name# 打印出来的是 ‘张三’ $ 的作用实际上是字符串拼接 #用于变量替换 那什么时候用$,什么时候 用 # (1 ...