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

需要注意的一些地方:

目录
相关文章
ListView Item多布局的实现
ListView这个小节的最后一节,给大家带来的是ListView多布局Item的实现, 何为ListView Item多布局,打个比方,QQ这种聊天列表
118 0
|
程序员 iOS开发 开发者
iOS开发:设置UICollectionView不同大小的item的方法
在iOS开发过程中,UICollectionView的使用作为iOS开发者来说都不陌生,但是要想完美的玩转UICollectionView的所有使用的技巧,还是需要了解很多的。本篇博文来分享一下关于UICollectionView设置不同大小item的方法,为的是迎合产品的需求,方便记录为了以后查看使用,分享给有需要的人。
884 0
iOS开发:设置UICollectionView不同大小的item的方法
|
容器
Fragment的replace、add、hide、show的使用和详解
Fragment的replace、add、hide、show的使用和详解
UITextView根据NSString计算Size
UITextView根据NSString计算Size
63 0
|
Android开发
NavigationView中,动态增加item以及menu
NavigationView中,动态增加item以及menu
357 0
ScrollView push之后再pop回来,contentOffset变成了0
ScrollView push之后再pop回来,contentOffset变成了0
188 0
Dialog显示引起的问题 Activity has leaked window DecorView@5704632[] that was originally added here
Dialog显示引起的问题 Activity has leaked window DecorView@5704632[] that was originally added here
1108 0
|
Windows
List View控件总结
List View控件总结
139 0