UINavigationController, uinavigationbar

Source: Internet
Author: User

UINavigationController, uinavigationbar


1. Use UINavigationController: add and remove controllers.

2. UINavigationBar content settings.

 Bytes ---------------------------------------------------------------------------------------------------------

1. add and remove controllers:

1. UINavigationController saves the sub-controller as a stack:

@ Property (nonatomic, copy) NSArray * viewControllers;

@ Property (nonatomic, readonly) NSArray * childViewControllers;


2. Use the push method to push a controller to the stack.

-(Void) pushViewController :( UIViewController *) viewController animated :( BOOL) animated;


/** Use the following method as a schematic */

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] bounds];
Self. window. backgroundColor = [UIColor whiteColor];

// 1. Create a navigation Controller
XZOneViewController * one = [[XZOneViewController alloc] init];
UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController: one]; // input a stack controller to initialize the navigation controller (commonly used)

// Get the stack top controller (display in front of the Controller)
// Nav. topViewController

// Store the stack of all sub-Controllers
// Nav. viewControllers
// This array also stores the sub-Controller
// Nav. childViewControllers

// 2. Add a sub-Controller
// XZOneViewController * one = [[XZOneViewController alloc] init];
// [Nav addChildViewController: one]; // You can also put one controller in the array viewControllers and childViewControllers.
// [Nav pushViewController: one animated: YES]; // push one to the stack, which is placed in viewControllers and childViewControllers. (Recommended usage, with animation)

// Nav. viewControllers = @ [one]; // This is also set
// Nav. viewControllers = @ [one]; // you cannot do this because viewControllers is read-only.

// 3. set as the root controller of the window
Self. window. rootViewController = nav;

[Self. window makeKeyAndVisible];
Return YES;


3. You can use the pop method to remove the controller from the stack. // you can remove the controller from the stack.

-(UIViewController *) popViewControllerAnimated :( BOOL) animated;


// Return to the specified sub-Controller

-(NSArray *) popToViewController :( UIViewController *) viewController animated :( BOOL) animated;


// Return to the root controller (stack Controller)

-(NSArray *) popToRootViewControllerAnimated :( BOOL) animated;

 4. The content in the navigation bar is determined by the navigationItem attribute of the stack top controller. The following attributes of UINavigationItem affect the content in the navigation bar. // the return button in the upper left corner.

@ Property (nonatomic, retain) UIBarButtonItem * backBarButtonItem;

// The title view in the middle

@ Property (nonatomic, retain) UIView * titleView;

// The title text in the middle

@ Property (nonatomic, copy) NSString * title;

// View in the upper left corner

@ Property (nonatomic, retain) UIBarButtonItem * leftBarButtonItem;


// View in the upper-right corner

@ Property (nonatomic, retain) UIBarButtonItem * rightBarButtonItem;


2. view Structure of the controller and settings of UINavigationBar navigation bar:

1. Scenario 1:

Self. navigationItem. title = @ "first controller ";

UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemTrash target: nil action: nil];
UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCompose target: nil action: nil];

Self. navigationItem. rightBarButtonItems = @ [item1, item2];
Self. navigationItem. leftBarButtonItems = @ [item1, item2];


2. Scenario 2:

Self. navigationItem. titleView = [UIButton buttonWithType: UIButtonTypeContactAdd];
Self. navigationItem. rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemTrash target: nil action: nil];

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.