一、UINavigationController
     1、UINavigationController:导航控制器,是iOS中最常用的多视图控制器之一,用它来管理多个视图控制器。可以称为是管理控制器的控制器,主要管理有层次递进关系的控制器。
     2、UINavigationController继承于UIViewController,以栈的方式管理所控制的视图控制器,至少要有一个被管理的视图控制器,这个控制器我们称作,导航控制器的根视图控制器。
  • 任何继承自UIViewController的类(多态)都可以作为根控制器。
     3、UINavigationController的创建
  在AppDelegate.m
    // 创建UIWindow对象
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    // 设置背景颜色
    self.window.backgroundColor = [UIColor whiteColor];

    // 使window显示
    [self.window makeKeyAndVisible];

    // 创建视图控制器,给window指定根控制器,设置导航控制器
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
二、UINavigationBar
     1、概述
  • UINavigationBar(导航栏)上的设置主要分两部分,一为导航栏上的各种导航部件(UINavigationItem),二为导航栏自身的相关设置。
  • navigationBar——导航条,iOS7之后默认是半透明的,iOS7之前默认是不透明的。
  • navigationBar竖屏下默认高度44,横屏下默认高度32。
  • iOS7之后,navigationBar的背景会延伸到statusBar上。导航栏高度仍保持44,但显示效果为64.
  • 每个视图控制器都有一个navigationItem属性。navigationItem中设置的左按钮、右按钮、标题等,会随着控制器的显示,也显示到navigationBar上。
     2、常用属性
  在RootViewController.m中
#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor brownColor];

    // 设置导航控制器
    [self initLayout];
}

// 初始化布局(实现布局的方法)
- (void)initLayout
{
    // 导航控制器的显示隐藏属性(YES == 隐藏, NO == 显示)
    self.navigationController.navigationBarHidden = NO;

    // UINavigationBar(导航条,iOS7.0 之后导航条默认有半透明属性)
    // 设置导航条是否开启半透明效果(会影响self.subViews的坐标系,当半透明效果开启时,self.view以屏幕左上角为坐标原点,关闭时,导航条左下角为坐标原点)
    self.navigationController.navigationBar.translucent = NO;

    // 修改导航条颜色,状态栏颜色默认与导航条颜色相同
    self.navigationController.navigationBar.barTintColor = [UIColor grayColor];

    // 导航条背景颜色
    self.navigationController.navigationBar.backgroundColor = [UIColor redColor];

    // 设置导航条标题
//    self.title = @"根视图";
    // navigationItem属性,用来设置子控件
    self.navigationItem.title = @"根视图";

    // 左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"左按钮" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAciton:)];

    // 右按钮
    // 一个按钮
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(rightItemClick:)];
//
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"next"] style:UIBarButtonItemStylePlain target:self action:@selector(rightItemClick:)];

    // 多个按钮, 自动排序

    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightItemClick:)];

    UIBarButtonItem *item2 =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(rightItemClick:)];

    self.navigationItem.rightBarButtonItems = @[item2, item1];

    // 设置导航元素的颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    // 导航栏样式
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;

    // 标题视图

    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"男神", @"女神", @"-1"]];
    segment.frame = CGRectMake(, , , );
    segment.selectedSegmentIndex = ;
    [segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];

    self.navigationItem.titleView = segment;

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
    view.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view];

}
// 实现左按钮方法
- (void)leftItemAciton:(UIBarButtonItem *)sender
{
    NSLog(@"跟着我左手 慢动作");
}
- (void)segmentAction:(UISegmentedControl *)sender
{
    switch (sender.selectedSegmentIndex) {
        :
            self.view.backgroundColor = [UIColor colorWithRed: green: blue:  alpha:];
            break;
        :
            self.view.backgroundColor = [UIColor colorWithRed: green: blue:  alpha:];
            break;
        :
            self.view.backgroundColor = [UIColor colorWithRed: green: blue:  alpha:];
            break;

        default:
            break;
    }

}

// 实现右按钮方法
- (void)rightItemClick:(UIBarButtonItem *)sender
{
    NSLog(@"跟着我右手 慢动作重播");
}
@end
     3、导航控制器左右按钮中英文的问题(例cancle / 取消),在工程设置->info->Localization native development region->China
三、页面跳转
     1、工作原理
  • UINavigationController通过栈的方式管理控制器的切换,控制入栈和出栈来展示各个视图控制器。
  • UINavigationController的ContentView里始终显示栈顶控制器的view。
  • viewControllers属性是一个可变数组(NSMutableArray)存储了栈中说的所有被管理的控制器,入栈的时候,使用addObject把新的视图控制器对象添加到数组末尾,出栈时removeLastObject移除数组末尾的视图控制器对象。
  • navigationController属性,父类中的属性,每个栈中的控制器,都能通过此属性,获取自己所在的UINavigationController对象。
  • 栈的特点:先进后出,后进先出。
     2、常用属性
    // 实现按钮方法
- (void)btnAction:(UIButton *)sender
{
    // 创建第一页的对象
    FirstViewController *fVC = [[FirstViewController alloc] init];
    // 通过导航控制器推出新的页面
    [self.navigationController pushViewController:fVC animated:YES];
}
- (void)popButtonAction:(UIButton *)sender
{
    // 返回上一个视图
    [self.navigationController popViewControllerAnimated:YES];

    // 返回指定视图
    [self.navigationController popToViewController:self.navigationController.viewControllers[] animated:YES];
    // 返回根视图
    [self.navigationController popToRootViewControllerAnimated:YES];
}
四、模态
     1、页面切换方式对比
  • 页面的切换方式主要分为:推出(push)和模态(present)。
  • 推出用于一系列的视图之间的跳转有层次递进关系。
  • 模态用于单独功能页面的跳转和主要业务逻辑没有关联(登录,歌曲播放页,系统相册,应用中调出系统功能)。
     2、添加过渡动画(有默认值)

- (void)modelBtnAction:(UIButton *)sender
{
    // 创建第二页对象
    ShowViewController *sVC = [[ShowViewController alloc] init];
    // 给第二页设置导航控制器
    UINavigationController *sNVC = [[UINavigationController alloc] initWithRootViewController:sVC];
    // 添加模态动画
    sVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    // 模态跳到下一个页面
    [self.navigationController presentViewController:sNVC animated:YES completion:nil];
}
- (void)backBtnAction:(UIButton *)sender
{
    // 模态返回上一个视图
    [self dismissViewControllerAnimated:YES completion:nil];
}
// 模态动画的效果// UIModalTransitionStylePartialCurl 翻页方式 推出
// UIModalTransitionStyleFlipHorizontal 水平旋转方式 推出
// UIModalTransitionStyleCrossDissolve 交叉叠化溶解方式 推出
// UIModalTransitionStyleCoverVertical 垂直覆盖方式 推出

iOS学习之UINavigationController的更多相关文章

  1. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  2. [转]iOS学习之UINavigationController详解与使用(三)ToolBar

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...

  3. [转]iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682433 iOS学习之UINavigationController详解与使用(一)添加U ...

  4. iOS学习之UINavigationController详解与使用(三)ToolBar

    1.显示Toolbar  在RootViewController.m的- (void)viewDidLoad方法中添加代码,这样Toobar就显示出来了. [cpp] view plaincopy [ ...

  5. iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController

    iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem是上篇,我们接着讲UINavigationController的重要作用,页面的管理和切换. ...

  6. iOS学习心得——UINavigationController

            UINavigationController和UItableviewController一样也是iOS开发中常用的控件之一,今天就来学习一下它的常见用法.         有人说tab ...

  7. [转]iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    转载地址:http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINav ...

  8. 2015最新iOS学习线路图

    iOS是由苹果公司开发的移动操作系统,以xcode为主要开发工具,具有简单易用的界面.令人惊叹的功能,以及超强的稳定性,已经成为iPhone.iPad 和iPod touch 的强大基础:iOS 内置 ...

  9. ios 学习路线总结

    学习方法 面对有难度的功能,不要忙着拒绝,而是挑战一下,学习更多知识. 尽量独立解决问题,而不是在遇到问题的第一想法是找人. 多学习别人开源的第三方库,能够开源的库一定有值得学习的地方,多去看别的大神 ...

随机推荐

  1. GLSL 中的光照计算

    理论知识转载地址:http://blog.csdn.net/ym19860303/article/details/25545933 1.Lambert模型(漫反射) 环境光: Iambdiff = K ...

  2. WM_INITDIALOG与WM_CREATE消息的区别

      WM_CREATE是所有窗口都能响应的消息,表明本窗口已经创建完毕(可以安全的使用这个窗口了,例如在它上面画控件等).在响应WM_CREATE消息响应函数的时候,对话框及子控件还未创建完成,亦是说 ...

  3. python字典的常用操作方法

    Python字典是另一种可变容器模型(无序),且可存储任意类型对象,如字符串.数字.元组等其他容器模型.本文章主要介绍Python中字典(Dict)的详解操作方法,包含创建.访问.删除.其它操作等,需 ...

  4. 简单的使用AngularJS的解析JSON

    使用AngularJS+Struts2进行前后台的数据交互与显示. struts.xml 配置文件需要将设置extends="json-default"  type="j ...

  5. Android IOS WebRTC 音视频开发总结(四三)-- 诚信交易案例分享

    本文主要记录一些诚信交易的案例(两个陌生人之间没有合同,没有订金,没有讨价还价,完全靠诚信完成的交易), 特别纪录下来并不是因为金额有多高,而是因为在现在这种社会要完成这样的交易太难,特别是像咨询这种 ...

  6. ASP.NET 项目 App_Code下无法找到类

    APP_CODE 默认情况下,VS2010中新建的WebApplication中是没有App_Code文件夹的,若需要使用,可以自己手动添加文件夹,然后将文件夹名称设置为App_Code,然后在该文件 ...

  7. URL重写以后发布到IIS找不到页面

    1.读取必须勾选,否则无法加载资源文件(img,css等) c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll

  8. 十九、利用OGNL获取ValueStack中:根栈和contextMap中的数据

    利用OGNL获取ValueStack中:根栈和contextMap中的数据 原则:OGNL表达式如果以#开头,访问的contextMap中的数据 如果不以#开头,是访问的根栈中的对象的属性(List集 ...

  9. C#局域网桌面共享软件制作(一)

    很久没有写文章了,今天分享的内容是局域网桌面共享软件,相信很多人都做过,也可能遇到一些问题,这里我将一一测试. 涉及到的内容有:局域网文件传输.桌面截屏.图片压缩等.为了更直白的演示程序的功能所以没有 ...

  10. css font-family 字体全介绍,\5b8b\4f53 宋体 随笔

    font-family采用一种"回退"的形式来保存字体,可以写若干种字体.当第一种字体浏览器不支持的时候,会找第二种字体,一次类推. font-family字体分为两类: 特殊字体 ...