UINavigationController_学习笔记

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:方法。

原文地址:https://www.cnblogs.com/greywolf/p/2582615.html