Navigation controller for IOS, navigation for ios

Source: Internet
Author: User

Navigation controller for IOS, navigation for ios

UINavigationController is the main tool used to build layered applications. It uses stacks to implement views. Any type of view controller can be put into the stack. When designing the navigation controller, you must specify the root view, that is, the first view that the user sees. The Root View Controller is the first View Controller pushed to the stack by the navigation controller. When you view the next attempt, A New View Controller is added to the stack, and the view controlled by the Controller is displayed to the user. You can use the navigation buttons to operate hierarchical applications and use them to control the push or release of views.

1. Common methods to add sub-controllers to navigation controllers

// Create a View Controller JRViewController * vc = [[JRViewController alloc] init]; // create a navigation controller, use the above controller as the root controller of the navigation controller UINavigationController * naVC = [[UINavigationController alloc] initWithRootViewController: vc]; // set the current navigation controller to the Root View Controller self in the window. window. rootViewController = naVC;

2. In this way, JRViewController is used as the root controller of the current navigation controller. Next we enter JRViewController and add a title and button Code as follows:

// Set the background self. view. backgroundColor = [UIColor redColor]; // set the title self. title = @ ""; // Add button UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (0, 0,100, 45)]; button. backgroundColor = [UIColor blackColor]; [button setTitle: @ "push" forState: UIControlStateNormal]; button. center = self. view. center; [button addTarget: self action: @ selector (pushVCNew) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: button];

As follows:

  3. Next we will jump between view controllers.

Navigation Controller Management View Controller mainly uses the method of stack pressure and stack exit. Let's take a look at how to switch. We added a click event to the button above, next we will repeat the method triggered by this click event:

-(Void) pushVCNew {// initialize the second controller SecondViewController * sec = [[SecondViewController alloc] init]; // switch to another View Controller [self. navigationController pushViewController: sec animated: YES];}

As follows:

After the stack is pressed, the response has an output stack. How can I return the output stack? Let's rewrite the return method of the second controller and call the Controller's popViewControllerAnimated method.

- (void) popVC{    [self.navigationController popViewControllerAnimated:YES];}

5. navigation buttons

// 1 constructor UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithImage: [UIImage imageNamed: @ "bt1"] style: Custom target: self action: @ selector (leftClick)]; // 2 custom view UIButton * button = [[UIButton alloc] initWithFrame: CGRectMake (100,100, 50, 40)]; button. backgroundColor = [UIColor redColor]; [button setTitle: @ "hello" forState: UIControlStateNormal]; UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithCustomView: button]; // 3 add to current navigationItem self. navigationItem. leftBarButtonItem = item2; self. navigationItem. rightBarButtonItem = item1; // 4 Add the title UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (0, 0,100, 40)]; label. text = @ "navigation controller"; label. textColor = [UIColor redColor]; self. navigationItem. titleView = label;

As follows:

6. ToolBar. Each navigation controller has a ToolBar, which is hidden by default. Next we will open the ToolBar and use it.

// Open toolbar self. navigationController. toolbarHidden = NO; // define the UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemCancel target: self action: @ selector (pushVC)]; UIBarButtonItem * item2 = [[UIBarButtonItem alloc] lower: lower target: self action: @ selector (pushVC)]; UIBarButtonItem * item3 = [[UIBarButtonItem alloc] lower: lower target: self action: @ selector (pushVC)]; UIBarButtonItem * item4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemOrganize target: self action: @ selector (pushVC)]; UIBarButtonItem * item5 = [[UIBarButtonItem alloc] lower: lower target: self action: @ selector (pushVC)]; UIBarButtonItem * item6 = [[UIBarButtonItem alloc] lower: lower target: self action: @ selector (pushVC)]; UIBarButtonItem * item7 = [UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: self action: @ selector (pushVC)]; UIBarButtonItem * item8 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: specified target: self action: @ selector (pushVC)]; item8.width = 20; // Add the button to the toolbar self. toolbarItems = @ [item1, item8, item2, item8, item3, item8, item4, item7, item5, item8, item6];

As follows:

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.