第一個(gè)項(xiàng)目之四--UINavigationController

再續(xù)前緣


這次還是對(duì)UITableView就是修修補(bǔ)補(bǔ)。這次給他加上一個(gè)UINavigationController,這個(gè)有什么用?字面意思啊!導(dǎo)航??!
當(dāng)你需要跳轉(zhuǎn)到下一個(gè)頁面的時(shí)候,用UINavigationController將下一個(gè)頁面放到棧頂。恩,就是這么用的。
<br />

又要對(duì)AppDelegate動(dòng)手啦


為了能使UITableView作為根視圖可以實(shí)現(xiàn)頁面跳轉(zhuǎn),我們就要對(duì)AppDelegate下手。將根視圖控制器由RootViewController更換為UINavigationController,再將RootViewController作為UINavigationController的根視圖。
大概關(guān)系就是這樣:
UIApplication -- UIWindow -- UINavigation -- RootView -- UITableView -- UITableViewCell

AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor grayColor];
    
    RootViewController *instance_rootView = [[RootViewController alloc]init];
    [instance_rootView.view setFrame:self.window.frame];
    
    UINavigationController *instance_navigation = [[UINavigationController alloc]initWithRootViewController:instance_rootView]; //初始化UINavigationController,并將RootViewController作為其根視圖
    
    self.window.rootViewController = instance_navigation; //再將UINavigationController作為UIWindow的根視圖
    [self.window makeKeyAndVisible];
    return YES;
}

現(xiàn)在Run一下,就能看到效果了:

效果

上面多了一條白色的條條,那個(gè)就是導(dǎo)航條了。導(dǎo)航條上面可以放什么?可以放按鈕、標(biāo)題等等很多。
但是這個(gè)好像不太美觀啊,我們做下全局樣式的變動(dòng)吧。
在此之前,我要先打包一個(gè)全局類,將一些比較常用,但是又麻煩的方法封裝在里面。

CommonHandler


CommonHandler.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface CommonHandler : NSObject

+(UIColor *)getColorWithRGB:(int)rgb;
+(UIColor *)getColorWithRed:(float)red andGreen:(float)green andBlue:(float)blue andAlpha:(float)alpha;

@end
CommonHandler.m
#import "CommonHandler.h"

#define RGB(r, g, b) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
#define RGBAlpha(r, g, b, a) [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)]

#define HexRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define HexRGBAlpha(rgbValue,a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:(a)]

@interface CommonHandler()

@end

@implementation CommonHandler

+(UIColor *)getColorWithRGB:(int)rgb{
    return HexRGB(rgb);
}
+(UIColor *)getColorWithRed:(float)red andGreen:(float)green andBlue:(float)blue andAlpha:(float)alpha{
    return RGBAlpha(red, green, blue, alpha);
}

@end

<br />

對(duì)導(dǎo)航條動(dòng)刀了


就這樣,一個(gè)快速獲取顏色的公共類就搞定了,比自帶的更方便了。
依舊是對(duì)AppDelegate動(dòng)手。

AppDelegate.m
[[UINavigationBar appearance] setBarTintColor:[CommonHandler getColorWithRGB:0xFFB6C1]];
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];

就加上這兩句,就完了。

Paste_Image.png

<br />

導(dǎo)入AlertMessage


前面因?yàn)闊o聊,弄了個(gè)AlertMessage自定義類,現(xiàn)在可以移植到這里來做測試了!
因?yàn)橐呀?jīng)打包好了,直接將兩個(gè)文件復(fù)制進(jìn)項(xiàng)目就可以直接用了。在這里我放出代碼:

AlertMessage.h
#import <UIKit/UIKit.h>

@interface AlertMessage : UIView

-(id)initWithY:(CGFloat)y andTitle:(NSString *)title;
-(id)initWithY:(CGFloat)y;
-(id)initWithTitle:(NSString *)title;
-(void)setStartWithY:(CGFloat)y;
-(void)setTitle:(NSString *)title;
-(void)showWithCover:(BOOL)boolean;
-(void)dismiss;
-(void)setTitleColor:(UIColor *)titleColor;
-(void)setAlertColor:(UIColor *)alertColor;
-(void)setAlertColor:(UIColor *)alertColor andTitleColor:(UIColor *)titleColor;
-(void)setDismissOnLeft:(BOOL)isOnLeft;
@end

然后就是實(shí)現(xiàn)文件

AlertMessage.m
#import "AlertMessage.h"

#define HEIGHT_ALERT 55
#define WIDTH_ALERT 180
#define Y_OFFSET_ALERT 100
#define X_OFFSET_ALERT (320 - WIDTH_ALERT)
#define HEIGHT_TITLE 25
#define GAP_DEFAULT 10
#define FONT_TITLE [UIFont systemFontOfSize:16]
#define RGB(r, g, b)    [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]

@interface AlertMessage()

@property (nonatomic, strong) NSString *string_title;

@property (nonatomic, strong) UIColor *color_title;
@property (nonatomic, strong) UIColor *color_background;

@property (nonatomic, strong) UILabel *lb_title;

@property (nonatomic, strong) UIButton *btn_cover;

@property (nonatomic, assign) BOOL isOnLeft;

@property (nonatomic, assign) CGFloat y;

@end

@implementation AlertMessage

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithY:(CGFloat)y andTitle:(NSString *)title{
    self = [self initAlert];
    if(self){
        [self setStartWithY:y];
        [self initContentWithTitle:title];
    }
    return self;
}

-(id)initWithY:(CGFloat)y{
    self = [self initAlert];
    if(self){
        [self setStartWithY:y];
    }
    return self;
}

-(id)initWithTitle:(NSString *)title{
    self = [self initAlert];
    if(self){
        [self initContentWithTitle:title];
    }
    return self;
}

-(id)initAlert{
    self = [self initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, Y_OFFSET_ALERT, WIDTH_ALERT, HEIGHT_ALERT)];
    if(self){
        self.y = Y_OFFSET_ALERT;
        self.backgroundColor = [UIColor blackColor];
        [self setAlpha:0.85];
        self.isOnLeft = NO;
    }
    return self;
}

-(void)setStartWithY:(CGFloat)y{
    self.y = y;
    self.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, self.y, WIDTH_ALERT, HEIGHT_ALERT);
}

-(void)setTitle:(NSString *)title{
    self.string_title = [NSString stringWithString:title];
    self.lb_title.text = self.string_title;
}

- (void)initContentWithTitle:(NSString *)title{
    self.string_title = [NSString stringWithString:title];
    self.lb_title = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width + GAP_DEFAULT, (HEIGHT_ALERT - HEIGHT_TITLE) / 2 + self.y, WIDTH_ALERT, HEIGHT_TITLE)];
    self.lb_title.text = self.string_title;
    self.lb_title.textColor = [UIColor whiteColor];
    self.lb_title.font = FONT_TITLE;
}

-(void)showWithCover:(BOOL)boolean{
    CGRect rect_cover = CGRectZero;
    if(boolean){
        rect_cover = [UIScreen mainScreen].bounds;
    }
    self.btn_cover = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.btn_cover setFrame:rect_cover];
    [self.btn_cover setBackgroundColor:[UIColor clearColor]];
    [self.btn_cover addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self.btn_cover addSubview:self];
    [self.btn_cover addSubview:self.lb_title];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.btn_cover];
    
    [UIView animateWithDuration:0.35f animations:^{
        [self setFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - WIDTH_ALERT, self.y, WIDTH_ALERT, HEIGHT_ALERT)];
        [self.lb_title setFrame:CGRectMake(self.frame.origin.x + GAP_DEFAULT, (HEIGHT_ALERT - HEIGHT_TITLE) / 2 + self.y, WIDTH_ALERT, HEIGHT_TITLE)];
    } completion:^(BOOL finished) {

    }];
}

-(void)dismiss{
    CGFloat endPoint;
    CGFloat duration;
    if(self.isOnLeft){
        duration = 0.5;
        endPoint = 0 - WIDTH_ALERT;
    }else{
        duration = 0.3;
        endPoint = [UIScreen mainScreen].bounds.size.width;
    }
    [UIView animateWithDuration:duration animations:^{
        [self setFrame:CGRectMake(endPoint, Y_OFFSET_ALERT, WIDTH_ALERT, HEIGHT_ALERT)];
        [self.lb_title setFrame:CGRectMake(self.frame.origin.x, (HEIGHT_ALERT - HEIGHT_TITLE) / 2 + self.y, WIDTH_ALERT, HEIGHT_TITLE)];
    } completion:^(BOOL finished) {
        [self.btn_cover removeFromSuperview];
        [self.lb_title removeFromSuperview];
    }];
}

-(void)setTitleColor:(UIColor *)titleColor{
    self.color_title = titleColor;
    self.lb_title.textColor = self.color_title;
}

-(void)setAlertColor:(UIColor *)alertColor{
    self.color_background = alertColor;
    self.backgroundColor = self.color_background;
    [self setAlpha:0.9];
}

-(void)setAlertColor:(UIColor *)alertColor andTitleColor:(UIColor *)titleColor{
    [self setTitleColor:titleColor];
    [self setAlertColor:alertColor];
}

-(void)setDismissOnLeft:(BOOL)isOnLeft{
    self.isOnLeft = isOnLeft;
}

@end

只要把新建一個(gè)新的Cocoa Touch Class類,然后把上面代碼復(fù)制進(jìn)行就好了。
在測試之前,我們還需要實(shí)現(xiàn)UITableView的點(diǎn)擊響應(yīng)事件。

RootViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s || section: %ld and row: %ld of cell's been selected.", __FUNCTION__, indexPath.section, indexPath.row);
    [self showAlertWithSection:indexPath.section andRow:indexPath.row];
}

- (void)showAlertWithSection:(NSInteger)section andRow:(NSInteger)row{
    NSString *string_temp = [NSString stringWithFormat:@"%@ - %@", self.array_section[section], [self.array_row[section] objectAtIndex:row]];
    AlertMessage *alert = [[AlertMessage alloc]initWithTitle:string_temp];
    [alert setAlertColor:[CommonHandler getColorWithRGB:0xFFE4C4]];
    [alert showWithCover:YES];
}

在底下加上這兩個(gè)方法,就完成了。現(xiàn)在就可以測試下效果了!

AlertMessage.gif

跳轉(zhuǎn)頁面


由于時(shí)間關(guān)系,這次就先跳一個(gè)空白的頁面好了!
我們先Command+N新建一個(gè)空白的UIViewController。
這個(gè)類我打算作為后續(xù)項(xiàng)目插件,所以就叫他為RouteViewController好了。
先導(dǎo)入剛新建的文件到RootViewController里面,然后修改initData,新加一個(gè)分組。然后再在didSelectedRowAtIndexPath:里面改改就完成了。

RootViewController.m
#import "RouteViewController.h"
...
- (void)initData{
    self.array_section = [NSArray arrayWithObjects:@"自定義消息提醒", @"自定義路線插件", nil];
    
    self.array_row = [NSArray arrayWithObjects:
                      [NSArray arrayWithObjects:@"Show Alert", nil],
                      [NSArray arrayWithObjects:@"Show View", nil],
                      nil];
}
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s || section: %ld and row: %ld of cell's been selected.", __FUNCTION__, indexPath.section, indexPath.row);
    switch (indexPath.section) {
        case 0:
            [self showAlertWithSection:indexPath.section andRow:indexPath.row];
            break;
        case 1:
            [self gotoRouteView];
            break;
        default:
            break;
    }
}
...
- (void)gotoRouteView{
    RouteViewController *instance_routeView = [[RouteViewController alloc]init];
    [self.navigationController pushViewController:instance_routeView animated:YES];
}

先初始化RouteViewController,然后通過UINavigationController里面的pushViewController方法來將RouteViewController放到棧頂,并顯示到屏幕上。
<br />

補(bǔ)充一點(diǎn)


之前對(duì)RootViewController的封裝的時(shí)候,因?yàn)槭韬?,還有一個(gè)地方?jīng)]有打包好,現(xiàn)在補(bǔ)上來:

RootViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.array_row[section] count];
}

那么現(xiàn)在就來看看跳轉(zhuǎn)頁面的效果吧!是不是很期待呢!我也是??!

ShowView.gif

相信都看到了,在跳轉(zhuǎn)頁面之后,是一片灰(其實(shí)是空內(nèi)容),雖然初始化了RouteViewController,但是并沒有在RouteViewController里面寫任何代碼,所以就會(huì)出現(xiàn)這樣的效果。
<br />

睡覺之前


今天就暫到這里,明天就開始寫一下Route插件!祝福我。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,321評(píng)論 4 61
  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,730評(píng)論 1 14
  • 赤腳大仙說了什么呢? “臣蒙王母詔見昨日赴會(huì),偶遇齊天大圣,對(duì)臣言萬歲有旨,著他邀臣等先赴通明殿演禮,方去赴會(huì)?!?..
    伯誠閱讀 600評(píng)論 0 2
  • 創(chuàng)業(yè)者的必要條件:合適的團(tuán)隊(duì)架構(gòu)、優(yōu)秀的員工、對(duì)未來強(qiáng)烈的愿景、甘冒風(fēng)險(xiǎn)的勇氣。++++流程。 新創(chuàng)企業(yè):是一個(gè)由...
    阿東咚咚咚閱讀 467評(píng)論 0 1
  • 在中國,有個(gè)地方被人們稱之為“第三極”。那里是圣潔的佛教圣地,也是有著各種奇麗雪山和峽谷的地方。那就是——西...
    藍(lán)文希閱讀 274評(píng)論 0 0

友情鏈接更多精彩內(nèi)容