1. 程式人生 > >貓貓學iOS 之微博項目實戰(2)微博主框架-自己定義導航控制器NavigationController

貓貓學iOS 之微博項目實戰(2)微博主框架-自己定義導航控制器NavigationController

點擊狀態 reat obj mar all func 返回 mutable point

貓貓分享,必須精品

原創文章。歡迎轉載。轉載請註明:翟乃玉的博客
地址:

viewmode=contents">http://blog.csdn.net/u013357243?viewmode=contents

一:加入導航控制器

上一篇博客完畢了對底部的TabBar的設置,這一章我們完畢自己定義導航控制器(NYNavigationController)。

為啥要做自己定義呢,由於為了更好地封裝代碼,而且系統的UINavigationController不能滿足我們的需求了,所以得自己定義。

首先,我們在NYTabBarViewController的
- (void)addChildVc:(UIViewController )childVc title:(NSString

)title image:(NSString )image selectedImage:(NSString )selectedImage方法中寫了這個:

 // 先給外面傳進來的小控制器 包裝 一個導航控制器
    NYNavigationController *nav = [[NYNavigationController alloc] initWithRootViewController:childVc];
    // 加入為子控制器
    [self addChildViewController:nav];

來給設置的各個Controller包裝一個導航控制器。

這時候系統會自己主動給加入一個。

然後呢我們要對導航控制器進行改進。

框架結構

眼下情況下的UI架構例如以下圖所看到的:一個IWTabBarController擁有4個導航控制器作為子控制器。每一個導航控制器都有自己的根控制器(棧底控制器)
技術分享


重要代碼

1.給控制器包裝一個導航控制器而且放入tabbarController中

// 先給外面傳進來的小控制器 包裝 一個導航控制器
    NYNavigationController *nav = [[NYNavigationController alloc] initWithRootViewController:childVc];
    // 加入為子控制器
[self addChildViewController:nav];

二:導航控制器左右item的設置

在NYMessageCenterViewController中我們加入了cell。並使之能夠點擊,點擊後進入到還有一個界面(test1) 再點擊界面的view進入另外一個界面(test2)

首先放入20行假數據——UITableView的數據源方法

返回一組,20行。每行內容cell設置

#pragma mark - Table view data source 數據源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 20;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"ID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"test~~~~message - %d", indexPath.row];
    return cell;
}

然後是cell的點擊方法了 不用死記全部方法名字,簡單敲一下tableview 查找didSele方法(學iOS對英語挺高老快了)靈活運用xcode的自己主動提示功能。

#pragma mark - 代理方法
//cell的點擊事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NYTest1ViewController *test1 = [[NYTest1ViewController alloc] init];
    test1.title = @"測試1控制器";


    [test1.navigationController setNavigationBarHidden:NO];

    [self.navigationController pushViewController:test1 animated:YES];
}

test1是我們自己做的一個測試類。當中我們做了兩個如圖:
技術分享

這時候,我們的消息界面就有了cell的數據而且能夠點擊了。如圖效果:
技術分享
(到test2的push和1的一樣,只是是用的view的touch方法)

這時候我們要做導航控制器的左右item了。
然後我們設置導航控制器的左右item (寫私信button等)
如圖:
技術分享
技術分享
技術分享

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"寫私信" style:UIBarButtonItemStylePlain target:self action:@selector(composeMsg)];

    //設置右側button為不可點擊狀態
    self.navigationItem.rightBarButtonItem.enabled = NO;

    NYLog(@"NYMessageCenterViewController-viewDidLoad");

}

當中的UIBarButtonItem 的創建方法不是系統給的,而是我們為了實現黃色的效果自己寫的分類實現的。

分類實現UIBarButtonItem的自己定義創建方法:


//
//  UIBarButtonItem+Extension.m
//  貓貓微博
//
//  Created by apple on 15-6-4.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"

@implementation UIBarButtonItem (Extension)

/**
 *  創建一個item
 *
 *  @param target    點擊item後調用哪個對象的方法
 *  @param action    點擊item後調用target的哪個方法
 *  @param image     圖片
 *  @param highImage 高亮的圖片
 *
 *  @return 創建完的item
 */
+(UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage
{

    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //設置圖片
    [backBtn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    [backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    //設置尺寸
    CGSize imageSize = backBtn.currentBackgroundImage.size;
    backBtn.frame = CGRectMake(0, 0, imageSize.width, imageSize.height);
    UIBarButtonItem *itemBtn = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    return itemBtn;

}

@end

這裏設置尺寸用到了

CGSize imageSize = backBtn.currentBackgroundImage.size;

在我們學習UI的transform的時候,我們知道 是不能直接這麽設置size的,可是為啥這裏能呢? 非常easy ,我們對UIView寫了一個分類

//
//  UIView+Extension.m
//  貓貓微博
//
//  Created by apple on 15-6-2.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "UIView+Extension.h"

@implementation UIView (Extension)
-(void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

-(CGFloat)x
{
    return self.frame.origin.x;
}
-(void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

-(CGFloat)y
{
    return self.frame.origin.y;
}

-(void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

-(CGFloat)width
{
    return self.frame.size.width;
}

-(void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

-(CGFloat)height
{
    return self.frame.size.height;
}

-(void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

-(CGSize)size
{
    return self.frame.size;
}

-(void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

-(CGPoint)origin
{
    return self.frame.origin;
}



@end

技術分享
技術分享
而且為了改變系統原生的 漂亮的藍色情調,換成微博的黃色。。


技術分享

我們要重寫NYNavigationController初始載入方法 (initialize)以及重寫pushViewController方法,讓push 的時候會自己主動帶著箭頭button和右邊的很多其它button(UIBarButtonItem)

技術分享

//
//  NYNavigationController.m
//  貓貓微博
//
//  Created by apple on 15-6-4.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYNavigationController.h"

@interface NYNavigationController ()

@end

@implementation NYNavigationController

+(void)initialize
{
    // 設置整個項目全部item的主題樣式
    UIBarButtonItem *item = [UIBarButtonItem appearance];


    // 普通狀態
    NSMutableDictionary *textAttrsNormal = [NSMutableDictionary dictionary];
    textAttrsNormal[NSForegroundColorAttributeName] = [UIColor orangeColor];
    textAttrsNormal[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    [item setTitleTextAttributes:textAttrsNormal forState:UIControlStateNormal];

    // 不可用狀態
    NSMutableDictionary *textAttrsDisabled = [NSMutableDictionary dictionary];
    textAttrsDisabled[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    textAttrsDisabled[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.7];
    [item setTitleTextAttributes:textAttrsDisabled forState:UIControlStateDisabled];

}

/**
 *  重寫這種方法目的:能夠攔截全部push進來的控制器
 *
 *  @param viewController 即將push進來的控制器
 */
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{

    // 這時push進來的控制器viewController,不是第一個子控制器(不是根控制器)
    if (self.viewControllers.count > 0) {
        /* 自己主動顯示和隱藏tabbar */
        viewController.hidesBottomBarWhenPushed = YES;

        // 設置左邊的箭頭button
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(back) image:@"navigationbar_back" highImage:@"navigationbar_back_highlighted"];
        // 設置右邊的很多其它button
        viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(more) image:@"navigationbar_more" highImage:@"navigationbar_more_highlighted"];
    }


    [super pushViewController:viewController animated:animated];



}

-(void)back
{
#warning 這裏要用self,不是self.navigationController
    // 由於self本來就是一個導航控制器,self.navigationController這裏是nil的
    [self popViewControllerAnimated:YES];
}

-(void)more
{
    //回到根
    [self popToRootViewControllerAnimated:YES];
}
@end

最後就是各個頁面的調用了
首頁:
技術分享

- (void)viewDidLoad
{
    [super viewDidLoad];

    /* 設置導航欄上面的內容 */
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(friendSearch) image:@"navigationbar_friendsearch" highImage:@"navigationbar_friendsearch_highlighted"];

    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(pop) image:@"navigationbar_pop" highImage:@"navigationbar_pop_highlighted"];

}

我:
技術分享


- (void)viewDidLoad
{
    [super viewDidLoad];

   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"設置" style:0 target:self action:@selector(setting)];
}

消息裏面的寫私信(這裏設置默認不可用狀態)
技術分享

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"寫私信" style:UIBarButtonItemStylePlain target:self action:@selector(composeMsg)];

    //設置右側button為不可點擊狀態
    self.navigationItem.rightBarButtonItem.enabled = NO;

    NYLog(@"NYMessageCenterViewController-viewDidLoad");

}

貓貓學iOS 之微博項目實戰(2)微博主框架-自己定義導航控制器NavigationController