iOS Development Project-03 adding a navigation controller
A simple explanation
Analysis: To analyze the microblog application, we need to add a navigation controller to each sub-controller (each sub-controller navigation is not the same), so we need to create a new navigation controller, and then the navigation controller as the window's root controller, add four sub-controller, respectively, added on the navigation controller, This means that the entire project uses the current mainstream UI framework, one Uitabbarcontroller manages four uinavigationcontroller, and each Uinavigationcontroller manages "home", "message", " Discover "and" me "of these four sub-controllers.
(1) Create a new class to inherit from Uinavigationcontroller.
(2) Use the navigation controller as the root controller of window
(3) Set title
Tips:
ChildVc.tabBarItem.title = title; ---------set the caption on the Tabbar label
ChildVc.navigationItem.title = title; ----Set the title of the navigation bar
Childvc.title = title,-----------------------equivalent to both Tabbaritem.title and Navigationitem.title
(4) New requirements, when clicking on the Sub Controller (TableView) of the cell when jumping to the next interface to hide the Tabbar toolbar.
Hide the Bar toolbar of the controller, you can intercept the push method, customize the navigation controller, override the Push method, you can intercept all the push incoming sub-controller
Intercept Push Operation ~
Second, the implementation of the Code
1. Code
In the host controller, the YYTABBARVIEWCONTROLLER.M file
1//2//YYTABBARVIEWCONTROLLER.M 3//02-Weibo add sub controller and set item structure 4//5//Created by Apple on 14-7-3. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYTabBarViewController.h" #import "YYHomeTableViewController.h" #import "Yydiscoverviewcontroller . h "#import" YYMessageViewController.h "#import" YYProfileViewController.h "#import" Uiimage+extension.h "15 # Import "YYNavigationViewController.h" @interface Yytabbarviewcontroller () @end20 @implementation YYTabBarViewController22-(void) VIEWDIDLOAD25 {+ [super viewdidload];27//Add four sub-controllers-Yyhometableviewco Ntroller *home=[[yyhometableviewcontroller alloc]init];29 [self addonechildvc:home title:@ "Home" imageName:@ "Tabbar_ Home "selectedimagename:@" tabbar_home_selected "];30 Yymessageviewcontroller *message=[[yymessageviewcont Roller alloc]init];33 [self addonechildvc:message title:@ "message" imagename:@ "Tabbar_message_center" selectedimagename:@ "Tabbar_message_center_selected "];34 yydiscoverviewcontroller *discover=[[yydiscoverviewcontroller alloc]init];36 [self add Onechildvc:discover title:@ "found" imagename:@ "Tabbar_discover" selectedimagename:@ "tabbar_discover_selected"];37 38 Yyprofileviewcontroller *profile=[[yyprofileviewcontroller alloc]init];39 [self addonechildvc:profile title:@ "Me" imagename:@ "Tabbar_profile" selectedimagename:@ "tabbar_profile_selected"];40}41 42/**43 * Add a sub-controller *45 * @param c HILDVC Control Object * @param title * @param imageName icon * @param selectedimagename Icon When selected */50-(void) ADDONECHILDVC: (Uiviewcontroller *) CHILDVC title: (NSString *) title ImageName: (NSString *) ImageName selectedimagename: (NSString *) SelectedImageName51 {52//Set the background color of the sub controller randomly childvc.view.backgroundcolor=yyr ANDOMCOLOR;54 55//Set title: Childvc.title=title; Equivalent to set the title of the latter both//childvc.navigationitem.title=title;//set the title of the navigation bar//childvc.tabbaritem.title=title;//set Tabbar above title 59 60//Set icon Childvc.tabbaritem.image=[uiimage imagewithname:imagename];62//Set the figure when selected Mark UIImage *selectedimage=[uiimage imagewithname:selectedimagename];64 (iOS7) {67//DECLARE this Picture with original (don't render) SelectedImage = [SelectedImage imagewithrenderingmode:uiimagerenderingmodealwaysoriginal];69}70 ChildVc.tabBarItem.selectedImage = selectedimage;71 72//Added as a sub-controller for Tabbar controller Yynavigationviewcontroller *nav=[[yynavigationviewcontroller alloc]initwithrootviewcontroller:childvc];74 [Self addChildViewController:na V];76 77}78 79 80//In iOS7, the image of SelectedImage is rendered again as blue 81///To display the original, you must tell it: Do not render the add-ons/Xcode installation path:/users/User name/libr Ary/application support/developer/shared/xcode/plug-ins84 @end
Code to intercept the push operation in a custom navigation controller
1//2// YYUINAVIGATIONVIEWCONTROLLER.M 3// 03-Weibo Add Navigation function 4//5// Created by Hole Doctor on 14-7-3.6// Copyrig HT (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYNavigationViewController.h" @interface Yynavigationviewcontroller () @end14 @implemen Tation YYNavigationViewController16-(void) viewDidLoad18 { [Super viewdidload];20}21-(void) Pushviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) Animated23 { //If the current push is not a stack top controller, So long hidden Tabbar tool strip if (self.viewcontrollers.count>0) { viewcontroller.hidesbottombarwhenpushed=yes; }28 [Super Pushviewcontroller:viewcontroller animated:yes];29}30 @end
Home controller, implementation code for the YYHOMETABLEVIEWCONTROLLER.M file
1//2//YYHOMETABLEVIEWCONTROLLER.M 3//02-Weibo add sub controller and set item structure 4//5//Created by Apple on 14-7-3. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYHomeTableViewController.h" @interface Yyhometableviewcontroller () @end14 @implementatio N YYHomeTableViewController16-(ID) Initwithstyle: (Uitableviewstyle) style18 {self = [super Initwithstyle:style]; if (self) {+//Custom initialization22}23 return self;24}25-(void) ViewDidLoad27 {[s Uper viewdidload];29}30 #pragma mark-table View data source32-(Nsinteger) TableView: (UITableView *) TableView number Ofrowsinsection: (Nsinteger) section33 {return 20;35}36 PNS-(UITableViewCell *) TableView: (UITableView *) TableView C Ellforrowatindexpath: (Nsindexpath *) indexPath38 {$ static NSString *id = @ "cell"; UITableViewCell *cell = [tabl Eview dequeuereusablecellwithidentifier:id];41 if (!cell) {$ = [[UITableViewCell alloc] IniTwithstyle:uitableviewcellstylesubtitle reuseidentifier:id];43}44 cell.textLabel.text = [NSString Stringwithforma t:@ "%d----home test data", indexpath.row];45 return cell;46}47-(void) TableView: (UITableView *) TableView Didselectrowatin Dexpath: (Nsindexpath *) indexPath49 {50//when clicking on the cell, skip to the next interface uiviewcontroller *NEWVC = [[Uiviewcontroller alloc] I nit];52 NewVc.view.backgroundColor = [Uicolor redcolor];53 newvc.title = @ "new controller"; [Self.navigationcontroller] PUSHVIEWCONTROLLER:NEWVC animated:yes];55}56 @end
2. Achieving results
(1) Start screen
(2) Program interface
System Home
Click on the first line (0) to jump to the new interface
The same is true of the message interface.
Description: This is all due to the interception of push operations in a custom navigation controller.
iOS Development Project-03 adding a navigation controller