码迷,mamicode.com
首页 > 其他好文 > 详细

UINavigationController便于pop的category

时间:2014-09-20 19:04:29      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   ar   for   2014   div   

UINavigationController便于pop的category

bubuko.com,布布扣

效果图:

bubuko.com,布布扣

这个category是为了方便UINavigationController用于跳转到指定的控制器当中,用于跳级,如果pop的控制器不存在,会直接提示:

bubuko.com,布布扣

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

需要注意的一些地方:

bubuko.com,布布扣

bubuko.com,布布扣

 

UINavigationController便于pop的category

标签:style   blog   http   color   io   ar   for   2014   div   

原文地址:http://www.cnblogs.com/YouXianMing/p/3983530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!