第一个项目之四--UINavigationController

再续前缘


这次还是对UITableView就是修修补补。这次给他加上一个UINavigationController,这个有什么用?字面意思啊!导航啊!
当你需要跳转到下一个页面的时候,用UINavigationController将下一个页面放到栈顶。恩,就是这么用的。
<br />

又要对AppDelegate动手啦


为了能使UITableView作为根视图可以实现页面跳转,我们就要对AppDelegate下手。将根视图控制器由RootViewController更换为UINavigationController,再将RootViewController作为UINavigationController的根视图。
大概关系就是这样:
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;
}

现在Run一下,就能看到效果了:

效果

上面多了一条白色的条条,那个就是导航条了。导航条上面可以放什么?可以放按钮、标题等等很多。
但是这个好像不太美观啊,我们做下全局样式的变动吧。
在此之前,我要先打包一个全局类,将一些比较常用,但是又麻烦的方法封装在里面。

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 />

对导航条动刀了


就这样,一个快速获取颜色的公共类就搞定了,比自带的更方便了。
依旧是对AppDelegate动手。

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

就加上这两句,就完了。

Paste_Image.png

<br />

导入AlertMessage


前面因为无聊,弄了个AlertMessage自定义类,现在可以移植到这里来做测试了!
因为已经打包好了,直接将两个文件复制进项目就可以直接用了。在这里我放出代码:

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

然后就是实现文件

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

只要把新建一个新的Cocoa Touch Class类,然后把上面代码复制进行就好了。
在测试之前,我们还需要实现UITableView的点击响应事件。

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];
}

在底下加上这两个方法,就完成了。现在就可以测试下效果了!

AlertMessage.gif

跳转页面


由于时间关系,这次就先跳一个空白的页面好了!
我们先Command+N新建一个空白的UIViewController。
这个类我打算作为后续项目插件,所以就叫他为RouteViewController好了。
先导入刚新建的文件到RootViewController里面,然后修改initData,新加一个分组。然后再在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 />

补充一点


之前对RootViewController的封装的时候,因为疏忽,还有一个地方没有打包好,现在补上来:

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

那么现在就来看看跳转页面的效果吧!是不是很期待呢!我也是啊!

ShowView.gif

相信都看到了,在跳转页面之后,是一片灰(其实是空内容),虽然初始化了RouteViewController,但是并没有在RouteViewController里面写任何代码,所以就会出现这样的效果。
<br />

睡觉之前


今天就暂到这里,明天就开始写一下Route插件!祝福我。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,884评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,212评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,351评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,412评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,438评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,127评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,714评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,636评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,173评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,264评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,402评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,073评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,763评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,253评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,382评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,749评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,403评论 2 358

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,117评论 4 61
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,492评论 1 14
  • 赤脚大仙说了什么呢? “臣蒙王母诏见昨日赴会,偶遇齐天大圣,对臣言万岁有旨,着他邀臣等先赴通明殿演礼,方去赴会。”...
    伯诚阅读 487评论 0 2
  • 创业者的必要条件:合适的团队架构、优秀的员工、对未来强烈的愿景、甘冒风险的勇气。++++流程。 新创企业:是一个由...
    阿东咚咚咚阅读 428评论 0 1
  • 在中国,有个地方被人们称之为“第三极”。那里是圣洁的佛教圣地,也是有着各种奇丽雪山和峡谷的地方。那就是——西...
    蓝文希阅读 229评论 0 0