iOS开发之-Navigation Controllers

Navigation Controllers



The Objects of a Navigation Interface




1. In addition, a navigation controller object automatically assigns itself as the delegate of its UINavigationBar object and prevents other objects from changing that relationship.

2. The navigation stack is a last-in, first-out collection of custom view controller objects that is managed by the navigation controller. The first item added to the stack becomes the root view controller and can never be removed. 




3. It is important to note that the objects in the topViewController and visibleViewController properties need not be the same. If you present a view controller modally from the topmost object in the stack, the topViewController property does not change but the object in the visibleViewController property does. Specifically, the value in the visibleViewController property changes to reflect the modal view controller that was presented.


Defining the Custom View Controllers for a Navigation Interface

1. One of the key things you must do to implement a navigation interface is decide what data you plan to present at each stage. For each level of your data hierarchy, you must provide a custom view controller object to manage and present the data at that level. 


 

Loading Your Navigation Interface from a Nib File


1. With a navigation controller, the view is always created programmatically so there is no view to put in a separate nib file. One consequence of this behavior is that a navigation controller never manages a nib file—in other words, you never assign a navigation controller to the File’s Owner placeholder. Instead, the only time you mix navigation controllers and nib files is when the navigation controller itself is stored in the nib file.




Creating a Navigation Interface Programmatically


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


Adopting a Full-Screen Layout for Navigation Views


1.  A view controller can ask that its view be displayed with a full-screen layout instead. In a full-screen layout, the content view is configured to underlap the navigation bar, status bar, and toolbar as appropriate.


2. When determining whether a view should be sized to fill all or most of the screen, a navigation controller considers several factors, including the following:

  • Is the underlying window (or parent view) sized to fill the entire screen bounds?
  • Is the navigation bar configured to be translucent?
  • Is the navigation toolbar (if used) configured to be translucent?
  • Is the underlying view controller’s wantsFullScreenLayout property set to YES?
3. If the navigation bar or toolbar are visible but not translucent, it does not matter if the view controller wants its view to be displayed using a full-screen layout. The navigation controller never displays content under an opaque navigation bar.

3. Tab bar views do not support translucency and tab bar controllers never display content underneath their associated tab bar. Therefore, if your navigation interface is embedded in a tab of a tab bar controller, your content may still underlap the navigation bar but will not underlap the tab bar.

4.  If you are creating a navigation interface and want your custom content to span most or all of the screen, here are the steps you should take:
  1. Configure the frame of your custom view to fill the screen bounds.
  2. Set the translucent property of your navigation controller to YES. This allows your content to underlap the navigation bar.
  3. To underlap the status bar, set the wantsFullScreenLayout property of your view controller to YES. (The navigation bar must be translucent in order for this attribute to be recognized.)
  4. To underlap an optional toolbar, set the translucent property of the toolbar to YES.

5. If you are presenting a navigation controller modally, the content presented by that navigation controller is limited by the view controller doing the presenting. If that view controller does not want to underlap the status bar, then the modally presented navigation controller is not going to be allowed to underlap the status bar either.


Modifying the Navigation Stack




Monitoring Changes to the Navigation Stack




1. You can use the methods of the navigation controller’s delegate to update the state of your navigation interface and modify your application’s data model. 


Customizing the Navigation Bar Appearance


A navigation bar is a container view that manages the controls commonly found in a navigation interface. 

Configuring the Navigation Item Object




 1. Although most of the navigation bar’s content is obtained from the topmost navigation item, a pointer to the back item is maintained so that a back button (with the title of the preceding item) can be created.

2. When used in a navigation interface, the contents of the navigation bar’s stack always parallel the contents of the parent navigation controller’s stack. In other words, for each view controller in the navigation stack, there is a corresponding navigation item in the same position on the navigation item stack of the navigation bar. 

3. A navigation bar has three primary positions for placing items: left, right, and center. 






Showing and Hiding the Navigation Bar


1. When used in conjunction with a navigation controller, you always use the setNavigationBarHidden:animated: method of UINavigationController to show and hide the navigation bar. You must never hide the navigation bar by modifying the UINavigationBar object’s hidden property directly. 

2. Because the user needs the back button on the navigation bar to navigate back to the previous screen, you should never hide the navigation bar without giving the user some way to get back to the previous screen. The most common way to provide navigation support is to intercept touch events and use them to toggle the visibility of the navigation bar. 

Modifying the Navigation Bar Object Directly


1. It is not permissible to change the navigation bar object or modify its bounds, frame, or alpha values directly. However, there are a few properties that it is permissible to modify, including the following: 1. 


2. The navigation bar automatically adjusts the content inset value to allow content to scroll out from under the navigation bar. It does not make this adjustment for other types of views.


Using Custom Buttons and Views as Navigation Items


1. To customize the appearance of the navigation bar for a specific view controller, modify the attributes of its associated UINavigationItem object. You can get the navigation item for a view controller from its navigationItem property. The view controller does not create its navigation item until you request it, so you should ask for this object only if you plan to install the view controller in a navigation interface.

2. For the topmost view controller, the item that is displayed on the left side of the navigation bar is determined using the following rules:

  • If you assign a custom bar button item to the leftBarButtonItem property of the topmost view controller’s navigation item, that item is given the highest preference.
  • If you do not provide a custom bar button item and the navigation item of the view controller one level down on the navigation stack has a valid item in its backBarButtonItem property, the navigation bar displays that item.
  • If a bar button item is not specified by either of the view controllers, a default back button is used and its title is set to the value of the title property of the previous view controller—that is, the view controller one level down on the navigation stack. 


3. For the topmost view controller, the item that is displayed in the center of the navigation bar is determined using the following rules:

  • If you assign a custom view to the titleView property of the topmost view controller’s navigation item, the navigation bar displays that view.
  • If no custom title view is set, the navigation bar displays a custom view containing the view controller’s title. 

4. For the topmost view controller, the item that is displayed on the right side of the navigation bar is determined using the following rules:

  • If the new top-level view controller has a custom right bar button item, that item is displayed. To specify a custom right bar button item, set the rightBarButtonItem property of the navigation item.
  • If no custom right bar button item is specified, the navigation bar displays nothing on the right side of the bar.

5. To add some custom prompt text above the navigation bar controls, assign a value to the prompt property of the navigation item.




Creating a Custom Bar Button Items

// View 3 - Custom right bar button with a view
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];    // keep track of this for later
 
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
 
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];

You should create the items in the viewDidLoad method of your view controller.



Using Edit and Done Buttons

1. The editButtonItem method of UIViewController returns a preconfigured button that when pressed toggles between an Edit and Done button and calls the view controller’s setEditing:animated: method with appropriate values. To add this button to your view controller’s navigation bar, you would use code similar to the following:

myViewController.navigationItem.rightBarButtonItem = [myViewController editButtonItem];

2. If you include this button in your navigation bar, you must also override your view controller’s setEditing:animated: method and use it to adjust your view hierarchy. 


Displaying a Navigation Toolbar


1. To configure a toolbar for your navigation interface, you must do the following:

  • Show the toolbar by setting the toolbarHidden property of the navigation controller object to NO.
  • Assign an array of UIBarButtonItem objects to the toolbarItems property of each of your custom view controllers



Specifying the Toolbar Items


1. When configuring bar button items, always remember to associate an appropriate target and action with the button. The target and action information is what you use to respond to taps in the toolbar. 

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


Showing and Hiding the Toolbar


1. To hide the toolbar for a specific view controller, set the hidesBottomBarWhenPushed property of that view controller to YES


2. If you want to hide the toolbar sometimes (but not always), you can call the setToolbarHidden:animated: method of the navigation controller at any time. 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值