18HWThread_UINavigationController_Title_Button_Hidden_UseStoryBoard

大纲

一、作业Homework_CircleMove0314
理论:不可在分线程中刷新UI,否则无法实现预期界面效果。

二、UINavigationController
项目:UINavigationController0315
理论:
栈:后进先出 队列:先进先出

导航(NavController):继承于UIViewController,根据栈的原理,实现多个视图的之间的切换功能。

界面跳转方法共4种:①root②模态弹出③
④UINavigationController:
步骤:
1.创建ViewController
2.创建NavigationController,且设置vc为nav的root
3.将nav设置为window的root
进栈:pushViewController: animated:
出栈:pop
方法1:(到上一vc)popViewController
方法2:(到root)popToRootViewController
方法3:(到任意vc)popToViewController

三、Navigation_Title_Button
项目:Navigation_Title_Button0315
理论:
1.显示导航栏标题
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title

2.添加导航按钮
2.1 系统提供的方式
①initWithBarButtonSystemItem: target: action:
②initWithTitle: style: target: action:
③initWithImage: style: target: action:
2.2 自定义方式
步骤:
1.创建button,设置其bounds/action等
2.声明UIBarButtonItem,并initWithCustomView:button
3.将button添加到nav

四、隐藏导航栏
项目:NavigationBar0315
理论:
设置导航隐藏:self.navigationController.navigationBarHidden = YES;
位置:一般设置在viewWillAppear:

五、使用storyBoard创建导航栏
项目:Navigation_UseStoryBoard0315
理论:
1.选中storyboard
2.点击Editor→Embed In→Navigation Controller

正文

一、作业Homework_CircleMove0314
理论:不可在分线程中刷新UI,否则无法实现预期界面效果。
源码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    //1.在圆心添加太阳
    UIImageView *sun = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fireball"]];
    sun.center = CGPointMake(CENTER_X, CENTER_Y);
    sun.bounds = CGRectMake(0, 0, 60, 60);
    [self.view addSubview:sun];
    //2.在圆周上添加地球
    UIImageView *earth = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"a"]];
    earth.center = CGPointMake(CENTER_X+RADIOUS, CENTER_Y);
    earth.bounds = CGRectMake(0, 0, 60, 60);
    [self.view addSubview:earth];
    //3.开启分线程
    [NSThread detachNewThreadSelector:@selector(newThread:) toTarget:self withObject:earth];
}
//分线程
//处理耗时数据
- (void)newThread:(UIImageView *)earth
{
 //无限循环
    for (; ; )
    {
        [NSThread sleepForTimeInterval:0.01];
        //根据角度,计算x,y坐标
        static float angle = 0;
        angle += 1;
        float huDu = angle / 180 * M_PI;
        _x = CENTER_X + RADIOUS * cos(huDu);
        _y = CENTER_Y + RADIOUS * sin(huDu);
        [self performSelectorOnMainThread:@selector(refreshUI:) withObject:earth waitUntilDone:NO];
    }
}
//主线程
//刷新UI
- (void)refreshUI:(UIImageView *)earth
{
    earth.center = CGPointMake(_x, _y);
}

二、UINavigationController
项目:UINavigationController0315
理论:
栈:后进先出 队列:先进先出

导航(NavController):继承于UIViewController,根据栈的原理,实现多个视图的之间的切换功能。

界面跳转方法共4种:①root②模态弹出③
④UINavigationController:
步骤:
1.创建ViewController
2.创建NavigationController,且设置vc为nav的root
3.将nav设置为window的root
进栈:pushViewController: animated:
出栈:pop
方法1:(到上一vc)popViewController
方法2:(到root)popToRootViewController
方法3:(到任意vc)popToViewController

源码:
文件:AppDelegate.m

    //1.创建视图控制器
    ViewController1 *vc1 = [[ViewController1 alloc]init];
    //2.创建导航控制器,设置vc1为导航的根视图控制器
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc1];
    //3.将nav作为window的根视图控制器
    self.window.rootViewController = nav;

文件:ViewController1.m

//下一页
//从vc1跳转到vc2,实现压栈(push/视图换入)
- (IBAction)nextClick:(UIButton *)sender
{
    //1.创建vc2
    ViewController2 *vc2 = [[ViewController2 alloc]init];
    //2.压栈,push
    [self.navigationController pushViewController:vc2 animated:YES];
}

文件:ViewController2.m

//从vc2跳转到vc1,实现视图的换出(pop/出栈)
//由3种方式
- (IBAction)backClick:(UIButton *)sender
{
    //pop
    //方法1:直接返回到上一级界面
    [self.navigationController popViewControllerAnimated:YES];
    //方法2:返回到根视图控制器
    [self.navigationController popToRootViewControllerAnimated:YES];
    //方法3:
    [self.navigationController popToViewController: animated:YES];
}
//跳转到vc3
- (IBAction)nextClick:(UIButton *)sender
{
    ViewController3 *vc3 = [[ViewController3 alloc]init];
    [self.navigationController pushViewController:vc3 animated:YES];
}

文件:ViewController4.m

//vc4到vc2
- (IBAction)backClick:(UIButton *)sender
{
    //第三种pop
    //先找到vc2
    //1.获取数组
    NSArray *vcArr = [self.navigationController viewControllers];
    //2.获取vc2
    ViewController2 *vc2 = [vcArr objectAtIndex:1];
    //3.popTo vc2
    [self.navigationController popToViewController:vc2 animated:YES];
}

三、Navigation_Title_Button
项目:Navigation_Title_Button0315
理论:
1.显示导航栏标题
1.1 方法1:self.title
1.2 方法2:self.navigationItem.title

2.添加导航按钮
2.1 系统提供的方式
1>initWithBarButtonSystemItem: target: action:
2>initWithTitle: style: target: action:
3>initWithImage: style: target: action:
2.2 自定义方式
步骤:
1.创建button,设置其bounds/action等
2.声明UIBarButtonItem,并initWithCustomView:button
3.将button添加到nav

源码:

    //1.显示导航标题
    //方法1:
    //title:Localized title for use by a parent controller
    self.title = @"主题";
    //方法2:
    //navigationItem:
    //The navigation item used to represent the view controller in a parent’��s navigation bar. (read-only)
    self.navigationItem.title = @"设置";
    
    //2.添加导航按钮
    //2.1 系统提供的几种方式
    //(1)系统图形按钮
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(photoClick:)];
    //self.navigationItem.rightBarButtonItem = item1;
    //(2)文字按钮
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteClick)];
//    self.navigationItem.leftBarButtonItem = item2;
    //(3)图片按钮
    //创建图片
    UIImage *image =  [UIImage imageNamed:@"scratch"];
    //设置图片渲染模式
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //使用渲染过后的图片
    UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(animationButton)];
    //创建item数组
    NSArray *itemArr = [[NSArray alloc]initWithObjects:item1,item3, nil];
    //将item数组赋给nav
    self.navigationItem.rightBarButtonItems = itemArr;
    
    //2.2 自定义
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //注意:位置坐标不起作用
//    button.center = CGPointMake(100, 30);
    button.bounds = CGRectMake(0, 0, 40, 40);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithCustomView:button];
    NSArray *itemArr2 = [[NSArray alloc]initWithObjects:item2,item4, nil];
    self.navigationItem.leftBarButtonItems = itemArr2;

四、隐藏导航栏
项目:NavigationBar0315
理论:
设置导航隐藏:self.navigationController.navigationBarHidden = YES;
位置:一般设置在viewWillAppear:

源码:

//在将要显示时,设置Hidden
//而不是在viewDidLoad设置
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

五、使用storyBoard创建导航栏
项目:Navigation_UseStoryBoard0315
理论:
1.选中storyboard
2.点击Editor→Embed In→Navigation Controller

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

推荐阅读更多精彩内容