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:20.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:20.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:20.f];
    titleAttribute.titleColor        = color;

    return titleAttribute;
}

@end

需要注意的一些地方:

时间: 2024-11-10 07:45:00

UINavigationController便于pop的category的相关文章

浅谈iOS Crash(二)

浅谈iOS Crash(一) 一.僵尸对象(Zombie Objects) 1.概述 僵尸对象:已经被释放掉的对象.一般来说,访问已经释放的对象或向它发消息会引起错误.因为指针指向的内存块认为你无权访问或它无法执行该消息,这时候内核会抛出一个异常( EXC ),表明你不能访问该存储区域(BAD ACCESS).(EXC_BAD_ACCESS类型错误) 调试解决该类问题一般采用NSZombieEnabled(开启僵尸模式). 2.使用NSZombieEnabled Xcode提供的NSZombie

微软面试题解析:栈的push、pop序列(栈)

题目:输入两个整数序列.其中一个序列表示栈的push顺序, 判断另一个序列有没有可能是对应的pop顺序. 为了简单起见,我们假设push序列的任意两个整数都是不相等的. 比如: 输入的push序列是1,2,3,4,5 ,那么4,5,3,2,1就有可能是一个pop序列. 因为可以有如下的push和pop序列: push 1, push 2, push 3, push 4, pop, push 5, pop, pop, pop, pop 这样的的得到的pop序列就是4,5,3,2,1. 但是序列4,

Android开发入门(二)使用意图 2.8 添加Category

通过使用Intent-Filter中的<category>元素,我们可以把activities进行分组.假设已经在 AndroidManifest.xml中添加了<category>元素: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" p

屏蔽响应事件继续向父视图传递的category

屏蔽响应事件继续向父视图传递的category 这篇教程是上一篇教程的升级版,将复杂的代码封装成了category,更便于使用:) 效果: 源码: UIGestureRecognizer+EnvetInCurrentView.h 与 UIGestureRecognizer+EnvetInCurrentView.m // // UIGestureRecognizer+EnvetInCurrentView.h // BackgroundView // // Created by YouXianMin

重载 UINavigationController 设置左侧返回按钮的文字为图片

UINavigationController 导航栏控制器的左侧返回按钮如果需要设置成图片,仅使用系统的是无法实现的,需要重载系统的导航栏控制器,在控制器推出之前替换掉leftBarButtonItem才行. 注:以下链接的这个哥们对NavigationViewController所有能做的定制都解说了 http://beyondvincent.com/blog/2013/11/03/120-customize-navigation-status-bar-ios-7/#5 源码如下: Custo

iOS开发UINavigation系列四——导航控制器UINavigationController

iOS开发UINavigation系列四--导航控制器UINavigationController 一.引言         在前面的博客中,我么你介绍了UINavigationBar,UINavigationItem和UIToolBar,UINavigationController是将这些控件和UIViewController紧密的结合了起来,使用导航,我们的应用程序层次会更加分明,对controller的管理也更加方便.前几篇博客地址如下: UINavigationBar:http://my

[翻译] POP Facebook的动画开源库

Pop is an extensible animation engine for iOS and OS X. In addition to basic static animations, it supports spring and decay dynamic animations, making it useful for building realistic, physics-based interactions. The API allows quick integration wit

用POP动画编写带富文本的自定义动画效果

用POP动画编写带富文本的自定义动画效果 [源码] https://github.com/YouXianMing/UI-Component-Collection   [效果]   [特点] * 支持富文本 * 可定制型强(继承父类重写父类的startAnimation方法即可) * 支持动画的中断与持续 * 支持CAMediaTimingFunction * 数据与UI隔离,便于你封装属于你的类   [核心] // // POPNumberCount.h // POP // // Created

PropertyGrid控件 分类(Category)及属性(Property)排序

最近在做表单设计器,设计器上的控件都是我们自己封装的,但每个属性类别里的属性是按照属性的拼音排序的,现在想按照PropertyIndex标识进行排序(PropertyIndex的后三位是用来标识编辑器的). 具体实现如下: using System; using System.Collections.Generic; using System.Text; using System.Reflection; using System.ComponentModel; using HC.Test.Com