Integrate TabBarController and NavigationController

Source: Internet
Author: User

I. project structure

Create an empty project

  

Ii. Main Code

1. AppDelegate. m

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {self. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]; UITabBarController * tabBarController = [[UITabBarController alloc] init]; firstViewController * one = [[FirstViewController alloc] init]; one. title = @ "Contact"; one. tabBarItem = [[UITabBarItem alloc] tags: custom tag: 0]; UINavigationController * navFirst = [[UINavigationController alloc] initWithRootViewController: one]; [tabBarController addChildViewController: navFirst]; secondViewController * two = [[SecondViewController alloc] init]; two. title = @ "favorites"; two. tabBarItem = [[UITabBarItem alloc] tags: custom tag: 1]; UINavigationController * navSecond = [[UINavigationController alloc] metadata: two]; [tabBarController addChildViewController: navSecond]; self. window. rootViewController = tabBarController; self. window. backgroundColor = [UIColor whiteColor]; [self. window makeKeyAndVisible]; return YES ;}

2. FirstViewController. m

-(Void) viewDidLoad {[super viewDidLoad]; UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] failed: UIBarButtonSystemItemAdd target: self action: @ selector (selectRightAction :)]; self. navigationItem. authorization = rightButton;}-(void) selectRightAction :( id) sender {ChildViewController * childViewController; childViewController = [[ChildViewController alloc] handler: @ "ChildViewController" bundle: nil]; childViewController. title = @ "add contact"; [self. navigationController pushViewController: childViewController animated: YES];}

3. SecondViewController. m

Default content

4. ChildViewController. m

Default content

Iii. Running Effect

1. Initial Interface

  

2. Go to the subview of the first view.

  

3. Switch to the second view.

  

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.