Article source: blog.csdn.net/rongxinhua/…

UINavigationController and UITabBarController are the two most commonly used view controllers in iOS development. They are both subclasses of UIViewController and inherit as follows:

[objc]  view plain copy

  1. @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>  
  2. @interface UINavigationController : UIViewController  

\

UINavigationController: a jump between pages of the same level. The typical feature of an interface is a UINavigationBar navigation bar at the top of the page. The navigation bar can set the title, the button in the upper left corner (generally used for returning), the button in the upper right corner, and you can also customize these elements.

UITabBarController: Nested relationship between parent and child pages. The typical feature of the interface is a UITabBar option group at the bottom. Click Tab to switch the views above. \

The integration of UIViewController, UINavigationController and UITabBarController can develop most App application page frames.

\

Add UIViewController to our project AppDelegate

[objc]  view plain copy

  1. // Add UIViewController to the application:
  2. – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  3. {  
  4.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  5.       
  6.     SplashViewController *splashViewController = [[SplashViewController alloc] initWithNibName:@”SplashViewController” bundle:nil];  
  7.     self.window.rootViewController = splashViewController;  
  8.       
  9.     self.window.backgroundColor = [UIColor whiteColor];  
  10.     [self.window makeKeyAndVisible];  
  11.     return YES;  
  12. }  

As mentioned, UINavigationController and UITabBarController are subclasses of UIViewController, so you can add them this way, depending on the project interface.

\

2. Jump and pass parameters between UIViewControllers

A typical application does not have a single page, and a jump between pages can be called like this:

[objc]  view plain copy

  1. // Jump from UIViewController to another UIViewController
  2. LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:@”LoginViewController” bundle:nil];  
  3. [self presentViewController:loginViewController animated:true completion:^{}];  
  4. // Return the last UIViewController from UIViewController
  5. [self dismissViewControllerAnimated:true completion:^{}];  

A lot of people who come from Android development ask a question: How do I pass parameters between two pages?

In fact, Android uses Intent objects to jump to and pass parameters. The current page is not an instance of the next page. In iOS, we create the next page instance directly, so you can provide a method in the next UIViewController instance for the current page to set parameters to.

In Android, the previous page is returned with an Intent. In iOS, parameters can be passed by proxy. See this in the example below.

\

Jump from UIViewController to UITabBarController

[objc]  view plain copy

  1. // Jump from UIViewController to UINavigationController
  2. HomeViewController *homeViewController = [[HomeViewController alloc] initWithNibName:@”HomeViewController” bundle:nil];  
  3. UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController];  
  4. [self presentViewController:navigationController animated:true completion:^{}];  

\

HomeViewController is a UITabBarController subclass with the following code:

[objc]  view plain copy

  1. //HomeViewController is a UITabBarController subclass
  2. @interface HomeViewController : UITabBarController<UITabBarControllerDelegate, HomeDelegate>  
  3. @end  

\

4. UITabBarController nested child pages

In this case, the UITabBarController is also a link in the UINavigationController navigation chain, so you can call the corresponding method of the navigation controller.

UITabBarController itself can be nested with multiple child pages, each page can be provided by a UIViewController. The code is as follows:

[objc]  view plain copy

  1. – (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5. // Set the navigation title
  6.         self.title = @”Message”;  
  7. // Set the button in the upper left corner of the navigation
  8.         UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@”Close” style:UIBarButtonItemStylePlain target:self action:@selector(onLeftBtnClick:)];  
  9.         self.navigationItem.leftBarButtonItem = leftBtn;  
  10. // Set the button in the upper right corner of the navigation
  11.         UIImage *img = [UIImage imageNamed:@”msgIcon”];  
  12.         UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:self action:nil];  
  13.         self.navigationItem.rightBarButtonItem = rightBtn;  
  14.   
  15. // Create a Tab page
  16.         UserListViewController *userListViewController = [[UserListViewController alloc] initWithNibName:@”UserListViewController” bundle:nil];  
  17.         MessageListViewController *messageListViewController = [[MessageListViewController alloc] initWithNibName:@”MessageListViewController” bundle:nil];  
  18. // Add Tab to the Tab controller
  19.         self.viewControllers = @[messageListViewController, userListViewController];  
  20. / / set UITabBarControllerDelegate agent
  21.         self.delegate = self;  
  22.     }  
  23.     return self;  
  24. }  

Switch between UITabBarController child pages \

HomeViewController UITabBarControllerDelegate agreement is achieved, can be used in the Tab when switching to perform this operation, as follows:

[objc]  view plain copy

  1. // Implement protocol methods to change the page title when switching tabs
  2. -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {  
  3.     NSInteger index = tabBarController.selectedIndex;  
  4.     NSString *title;  
  5.     switch (index) {  
  6.         case 0:  
  7.             title = @”Message”;  
  8.             break;  
  9.         case 1:  
  10.             title = @”User List”;  
  11.             break;  
  12.     }  
  13.     self.title = title;  
  14. }  

In the child page of UITabBarController (UIViewController instance), you can set the properties of the TabBar item corresponding to the child page as follows:

[objc]  view plain copy

  1. – (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  2. {  
  3.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  4.     if (self) {  
  5.         // Custom initialization  
  6.         self.tabBarItem.title = @”User List”;  
  7.         self.tabBarItem.image = [UIImage imageNamed:@”chatIcon01″];  
  8.     }  
  9.     return self;  
  10. }  

UITabBarController jumps to the next page of UINavigationController

Jump from the UITabBarController sub-page to the next page of the UINavigationController. Note that the UITabBarController is a node in the UINavigationController navigation chain.

[objc]  view plain copy

  1. // Jump from the subpage of UITabBarController to the next page of UINavigationController:
  2. ChatViewController *chatViewController = [[ChatViewController alloc] initWithNibName:@”ChatViewController” bundle:nil];  
  3. UITabBarController *homeController = self.tabBarController;  
  4. [chatViewController setHomeDelegate:homeController];  
  5. [self.tabBarController.navigationController pushViewController:chatViewController animated:true];  

Here we use proxy to implement parameter return, this proxy has the following advantages:

[objc]  view plain copy

  1. @protocol HomeDelegate <NSObject>  
  2. -(void) onComeback:(NSString*) message;  
  3. @end  

You need to add the agent as its property on the next page (ChatViewController in the example) as follows:

[objc]  view plain copy

  1. @interface ChatViewController : UIViewController  
  2. @property (weak) id<HomeDelegate> homeDelegate;  
  3. @end  
  4.   
  5. @implementation ChatViewController  
  6. @synthesize homeDelegate;  
  7. @end  

\

Seven, return to the previous page and parameter return

To return to the previous page from the ChatViewController, execute the following code:

[objc]  view plain copy

  1. [homeDelegate onComeback:@”Hello”];  
  2. [self.navigationController popViewControllerAnimated:true];  

In this way, the parameters can be returned.

\

UINavigationController has two common methods for page rollback:

[objc]  view plain copy

  1. // Return to the previous page of the navigation
  2. [self.navigationController popViewControllerAnimated:true];  
  3.   
  4. // Return to the first page of the navigation
  5. [self.navigationController popToRootViewControllerAnimated:true];  

—————

A typical iPhone application consists of a Window and several UIViewControllers, each of which manages multiple UIViews (possibly UITableView, UIWebView, UIIMageView, etc.). How these UIViews stack, show, hide, rotate, move, etc., is all handled by UIViewController, UITableViewController, or UISplitController.

When your application has a hierarchical workflow, it’s good to use UINavigationController to manage UIViewController. That is, the user can go from one interface to the next and then simply return to the next interface. UINavigationController uses a stack to manage UIViewController.

\

The code for entering the next layer is as follows:

[self.navigationController pushViewController:nextController animated:YES];

\

The code for returning to the previous interface is as follows:

[self.navigationController popViewControllerAnimated:YES];

\

When the application used pushViewController() to add ViewController to UINavigationController, the UINavigationController automatically added a (back) return button, Clicking the “back” button takes the user back to the original interface.

The mechanism of UINavigationController makes it convenient to organize the user interface in such a way that the user can go layer by layer into a deeper layer of the interface and then go back layer by layer.

\