http://site.douban.com/129642/widget/notes/5513129/note/187701199/

1-view controllers的关系:
Each custom view controller manages a distinct view hierarchy but a navigation controller coordinates the navigation between different view hierarchies.
2-Navigation Interface中的对象
你不能改变navigation controller的navigation bar和toolbar对象,navigation controller对象自己负责配置和显示它们。此外,navigation controller自动将自己设置为它自己的UINavigationBar对象的委托,并组织其它对象改变此关系。
Navigation stack是一个后进先出的custom view controller集合,由navigation controller负责管理。第一个被加入的会称为root view controller并且永远不会被移出stack。

topViewController指向stack顶部的view controller。
visibleViewController指向当前显示的view的controller。
这2者未必相同,有可能当先显示的view controller并未入stack。

你的主要职责是根据用户的行为,将新的view controllers push到stack中。
3-创建Navigation Interface
首先要决定在哪里使用navigation interface。
                ● Install it directly in your application’s main window. 
                ● Install it as the root view controller of a tab in a tab bar interface. 
                ● Install it as one of the two root view controllers in a split view interface. (iPad only) 
                
● Present it modally or otherwise use it as a standalone view controller that you can display and dismiss as needed. 
                ● Display it from a popover. (iPad only) 
然后为Navigation Interface定义custom view controllers
加载Navigation Interface
        [1] 拖拽一个navigation controller到IB的document window;
        [2] 保存一个到navigation controller的IBOutlet引用;
        [3] 设置navigation controller的root view controller,(在IB中)将其设置为自定义的类;
The class you choose should be the one responsible for displaying the highest-level data in your navigation hierarchy.
        [4] 配置root view controller的view;
        [5] 在applicationDidFinishLaunching:方法中,将你的navigation controller的view加入到main window;
        . - (void)applicationDidFinishLaunching:(UIApplication *)application {
        . [window addSubview:myNavigationController.view];
        . 
} 
        [6] 保存nib file;

编程创建navigation controller:
        [1] 创建root view controller;
        [2] 创建navigation controller,调用initWithRootViewController:方法;
        [3] 增加navigation controller的view到window;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    UIViewController *rootController = [MyRootViewController alloc] init];
    navigationController = [UINavigationController alloc]
                                initWithRootViewController:rootController];
    [rootController release];
    window = [UIWindow alloc] initWithFrame:[UIScreen mainScreen] bounds];
    [window addSubview:navigationController.view];
    [window makeKeyAndVisible];
}
修改Navigation Stack
        [1] 显示下一层数据:pushViewController: animated:方法;把新的view controller放到stack顶,该controller负责显示新的界面;
        [2] 返回体系中的上一个层次:popViewControllerAnimated:方法;
        [3] 将App恢复到上次退出前的状态:当App启动时,你可以使用setViewControllers: animated:
方法,恢复navigation controller的状态。为了恢复app到原来的状态,你必须保存足够的状态信息来重新创建需要的view controllers。下次启动时,需要读取这些状态信息来重新创建这些view controllers。之后才调用setViewControllers: animated:方法;
        [4] 返回到root view controller:popToRootView- ControllerAnimated:方法;此方法删除root view controller以外的所有的controllers;
        [5] 退多层:popToViewController: animated:方法。应用场景:当用户进行了一系列操作,想要撤销这些操作的时候可以调用此方法;
        [6] 跳到任意层次:setViewControllers: animated:方法,尽量别用;

4-监控Navigation Stack的变化
当navigation stack发生变化,navigation controller发送适当的消息给它的代理。
当新入stack一个view controller时,会向topmost view controller发送viewWillDisapear消息,向新的view controller发送viewWillAppear消息;
向委托发送navigationController: willShowViewController: animated:消息;
完成视图替换。
向topmost view controller发送viewDidDisappear:,向新的view controller发送viewDidAppear:消息;
向委托发送navigationController: didShowViewController: animated:消息;

5-定制Navigation Bar 外观
navigation bar是管理controls的容器视图。Navigation controller负责管理navigation bar。
        [1] 配置Navigation Item Object
                Navigation bar是采用navigation item stack保存UINavigationItem对象。每个navigation item提供一个完整的视图集合,以及在navigation bar中要显示的内容。navigation bar是一个纯粹的view对象,可以被嵌入到其它view中。UINavigationBar的委托是UINavigationController(如果尝试修改,会导致异常)。UINavigationBar中保存了以下指针:items、backItem、topItem。
                当使用一个navigation interface,navigation bar的stack一般是和navigation controller的stack并行的。这是1对1的关系。
                一个navigation bar有3个主要的位置用来放置items:左(backBarButtonItem-缺省,leftBarButtonItem-用以替换back按钮)、中(titleView)、右(rightBarButtonItem)。可以在rightBarButtonIte的contentView中定制右边的按钮,可以放按钮的集合。
        [2] 显示和隐藏Navigation Bar
                使用UINavigationController的setNavigationBarHidden:animated:方法显示或隐藏navigation bar。不能修改UINavigationBar对象的hidden属性。在viewWillAppear:中调用。
        [3] 修改Navigation Bar
                对于navigation interface,一个navigation controller拥有它自己的UINavigationBar对象,并且负责管理它。不允许改变navigation bar对象的bounds,frame,或者alpha等等。但是以下几个属性允许修改,包括:barStyle,translucent,tintColor属性。
        [4] 使用用户自定义Buttons和Views做Navigation Items
                你可以通过view controller的navigationItem属性,得到navigation item。view controller在你请求之前,不会创建它的navigation item。如果你不修改view controller的navigation item, navigation item会提供一系列缺省对象(可以满足多数需求)。当然,你定制的内容要优先于缺省对象。
                左侧区域的规则:
                1. 如果你配置了一个自定义的bar button item到( topmost view controller’s navigation item的)leftBarButtonItem属性,那么此item会得到最高优先级;
                2. 如果你没有配置bar button item(既没有做1.的操作),而且在navigation stack中处于当前view controller的下层的view controller的navigation item在其backBarButtonItem属性中有一个正确的item,navigation bar将显示该item;
                3. 如果1.2.都没有配置那么会自动配置一个缺省的back button,其title背设置为前一个view controller的title属性。
                中间区域的规则:
                1. 如果你配置了一个自定义view到topmost view controller’s navigation item的titleView,navigation bar 将显示该view;
                2. 如果没做1.的配置,navigation bar 会显示一个定制的view,包含view controller(注意,和1不同,1是navigation item的titleView)的navigation item的title,如果title是nil,则显示view controller自己的title;
                右侧区域的规则:
                1. 如果新的top-level view controller有一个定制的right bar button item,将显示该item。通过设置navigation item的rightBarButtonItem属性来定制;
                2. 如果没有定制right bar button item,将什么都不显示;
示例代码(viewDidLoad):
UISegmentedControl *segmentedControl = [UISegmentedControl alloc] initWithItems: [NSArray arrayWithObjects:
                                          [UIImage imageNamed:@"up.png"],
                                          [UIImage imageNamed:@"down.png"],
                                          nil];
[segmentedControl addTarget:self action:@selector(segmentAction:)
forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; for later
UIBarButtonItem *segmentBarItem = [UIBarButtonItem alloc]
initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];

[5] 使用Edit和Done Buttons
                UIViewController的editButtonItem方法返回一个预配置的button,按下此button会调用view controller的setEditing:animated:方法。增加此按钮:
                myViewController.navigationItem.rightBarButtonItem = [myViewController editButtonItem];
                另外要重写setEditing:animated:方法。

6-显示Navigation Toolbar
        toolbar由navigation controller对象。
        [1] 通过设置navigation controller的toolbarHidden属性为NO来显示boolbar。
        [2] 配置UIBarButtonItem对象数组到toolbarItems属性(每个需要toolbar的view controller都要单独定制)。

7-定义Toolbar Items
当配置bar button items时, 要为button配置一个合适的target(一般是view controller自身)和action。
示例代码:
        . - (void)configureToolbarItems
        . {
        . 
 UIBarButtonItem *flexibleSpaceButtonItem = [UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                        target:nil action:nil];
   // Create and configure the segmented control
   UISegmentedControl *sortToggle = [UISegmentedControl alloc]
                        initWithItems:[NSArray arrayWithObjects:@"Ascending",
                                        @"Descending", nil];
   sortToggle.segmentedControlStyle = UISegmentedControlStyleBar;
   sortToggle.selectedSegmentIndex = 0;
   [sortToggle addTarget:self action:@selector(toggleSorting:)
               forControlEvents:UIControlEventValueChanged];
   // Create the bar button item for the segmented control
   UIBarButtonItem *sortToggleButtonItem = [UIBarButtonItem alloc]
                                    initWithCustomView:sortToggle];
   [sortToggle release];
   // Set our toolbar items
   self.toolbarItems = [NSArray arrayWithObjects:
                         flexibleSpaceButtonItem,
                         sortToggleButtonItem,
                         flexibleSpaceButtonItem,
                         nil];
   [sortToggleButtonItem release];
   [flexibleSpaceButtonItem release];
}

可以通过setToolbarItems:animated:方法改变toolbar的设置。

显示和设置Toolbar
为了隐藏一个特定view controller的toolbar,设置hidesBottomBarWhenPushed属性为YES。(这个是在切换view controller时使用的方法)
当view controller替换完了,可以调用setToolbarHidden:animated:方法。

UINavigationController学习笔记的更多相关文章

  1. iOS学习笔记-自己动手写RESideMenu

    代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...

  2. IOS学习笔记37——ViewController生命周期详解

    在我之前的学习笔记中讨论过ViewController,过了这么久,对它也有了新的认识和体会,ViewController是我们在开发过程中碰到最多的朋友,今天就来好好认识一下它.ViewContro ...

  3. React Native 学习笔记--进阶(二)--动画

    React Native 进阶(二)–动画 动画 流畅.有意义的动画对于移动应用用户体验来说是非常必要的.我们可以联合使用两个互补的系统:用于全局的布局动画LayoutAnimation,和用于创建更 ...

  4. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  5. PHP-自定义模板-学习笔记

    1.  开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2.  整体架构图 ...

  6. PHP-会员登录与注册例子解析-学习笔记

    1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...

  7. 2014年暑假c#学习笔记目录

    2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...

  8. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  9. seaJs学习笔记2 – seaJs组建库的使用

    原文地址:seaJs学习笔记2 – seaJs组建库的使用 我觉得学习新东西并不是会使用它就够了的,会使用仅仅代表你看懂了,理解了,二不代表你深入了,彻悟了它的精髓. 所以不断的学习将是源源不断. 最 ...

随机推荐

  1. IOS UIWebView截获html并修改便签内容,宽度自适应

    需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果:  方法:通过js截获UIWebView中的html,然后修改html标签内容:  实例代码:  服 ...

  2. matlab实现复合梯形法则

    复合梯形法则: function int_f = CompoundEchelon( f, a, b, m ) % input : f : function handler % a : the lowe ...

  3. WPF中使用MVVM模式进行简单的数据绑定

    计划慢慢整理自己在WPF学习和工作应用中的一些心得和想法,先从一个简单的用法说起 在WPF中,XAML标记语言中绑定数据,而数据源就是指定为ViewModel类,而非界面本身的逻辑代码类 这样一定程度 ...

  4. Laravel5 路由问题 /home页面无法访问

    参考网址:http://stackoverflow.com/questions/11791375/laravel-routes-not-working Laravel5 路由问题 /home页面无法访 ...

  5. Team Homework #2

    王宇杰,北航计算机学院2011级本科生,学号11061180.本人没有什么特长,典型的死宅一个.学过一点c和java.对于第一次的个人作业就表示亚历山大,希望各位大神不吝分享自己的代码,并做好注释.希 ...

  6. Netsharp快速入门(之12) 销售管理(开发发货单工作区)

    作者:秋时 杨昶   时间:2014-02-15  转载须说明出处 4.4     发货单 4.4.1  部件工作区设置 1.设置部件工作区,需要设置的部件如下 2.设置单据和明细列表的字段,设置完成 ...

  7. 移动端页面使用rem来做适配

    文/九彩拼盘(简书作者)原文链接:http://www.jianshu.com/p/eb05c775d3c6著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. rem介绍 rem(font ...

  8. phonegap file操作

    phonegap中,有时候需要操作到手机中的文件,这个时候就需要用到phonegap官方提供的插件 file ,英文好的可以直接参考官方文档 首先是安装插件:(需要phonegap 3.0 以上,不止 ...

  9. [设计模式] 4 原型模式 prototype

    设计模式:可复用面向对象软件的基础>(DP)本文介绍原型模式和模板方法模式的实现.首先介绍原型模式,然后引出模板方法模式. DP书上的定义为:用原型实例指定创建对象的种类,并且通过拷贝这些原型创 ...

  10. PHP对XML文件操作之属性与方法讲解

    DOMDocument相关的内容. 属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataType 返回此节点的数据类型 Definit ...