Build a mainstream framework interface
-
When we played the iphone application, did not find that most of the applications are similar structure, the following Tabbar controller can switch the sub-controller, which has navigation navigation bar
-
This article is mainly to build the framework of the main body, the data is not added temporarily
Analyze the basic process of doing the project
Build a mainstream framework (pure code) starting from 0
1. Preparatory work
2. Initial setup of the basic interface
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {//1. Create Window Self.window = [[UIWindow alloc]initwithframe:[uiscreen mainscreen].bounds]; 2. Set the window's root controller Cyxtabbarcontroller *TABBARVC = [[Cyxtabbarcontroller alloc]init]; Self.window.rootViewController = TABBARVC; 3. display window [Self.window makekeyandvisible]; return YES;}
- (void) viewdidload { [super viewdidload]; // 1. Adding the first controller // 1.1 initialization CYXOneViewController *onevc = [[cyxoneviewcontroller alloc]init]; // 1.2 Add the ONEVC as the Uinavigationcontroller root controller uinavigationcontroller *nav1 = [[ uinavigationcontroller alloc]initwithrootviewcontroller:onevc]; // Set the title of Tabbar nav1.title = @ "Home"; [nav1.navigationbar setbackgroundimage:[uiimage imagenamed:@ "COMMENTARY_NUM_BG"] forbarmetrics:uibarmetricsdefault ]; // Setting the Tabbar icon nav1.tabbaritem.image = [ uiimage imagenamed:@ "Tab_home_icon"]; // set Navigationbar title oneVC.navigationItem.title = @ "Home"; // set background color (these actions can be given to each individual sub-controller to do) onevc.view.backgroundcolor = [uicolor whitecolor]; // 1.3 Handing Uinavigationcontroller to Uitabbarcontroller management [self addChildViewController:nav1]; // 2. Adding a 2nd controller cyxtwoviewcontroller *twovc = [[cyxtwoviewcontroller alloc]init]; uinavigationcontroller *nav2 = [[uinavigationcontroller alloc]initwithrootviewcontroller:twovc]; nav2.title = @ "Technology"; nav2.tabbaritem.image = [uiimage imagenamed:@ "JS"]; twovc.navigationitem.title = @ "Technology"; twovc.view.backgroundcolor = [uicolor bluecolor]; [self Addchildviewcontroller:nav2]; // 3. Adding a 3rd Controller &Nbsp; cyxthreeviewcontroller *threevc = [[cyxthreeviewcontroller alloc] init]; uinavigationcontroller *nav3 = [[uinavigationcontroller alloc]initwithrootviewcontroller:threevc]; nav3.title = @ "Blog"; nav3.tabbaritem.image = [uiimage imagenamed:@ "QW"]; threevc.navigationitem.title = @ "Bowen"; threevc.view.backgroundcolor = [UIColor yellowColor]; [self addChildViewController:nav3]; // 4. Adding a 4th controller cyxfourviewcontroller *fourvc = [[ cyxfourviewcontroller alloc]init]; uinavigationcontroller *nav4 = [ [uinavigationcontroller alloc]initwithrootviewcontroller:fourvc]; nav4.title = @ "My Lake"; nav4.tabbaritem.image = [uiimage imagenamed:@ "User"]; fourvc.navigationitem.title = @ "My Lake"; fourvc.view.backgroundcolor =  [UICOLOR GRAYCOLOR];    [SELF ADDCHILDVIEWCONTROLLER:NAV4];}
We've got the frame up here, isn't it simple? Effect
-
But you may be tempted to vomit the slot, these are all redundant garbage code, no readability, let's take a look at the code below
-
Fourth step, extracting duplicate code
-
Since all of the code above is written in viewdidload and repeated too much code, resulting in code redundancy, scalability is not high, let's take a preliminary optimization of the code.
-
Here we extract two methods, one is to add all the sub-controllers, and the other is to add a method for each child controller
- (void) viewdidload { [super viewdidload]; [self setupallchildviewcontroller];} /** * Add all Child controller methods */- (void) setupallchildviewcontroller{ // 1. Adding a first controller cyxoneviewcontroller *onevc = [[cyxoneviewcontroller alloc]init]; [self setuponechildviewcontroller:onevc image:[uiimage imagenamed:@ "Tab_home_icon"] title:@ "Home"]; // 2. Add a 2nd Controller CYXTwoViewController *twoVC = [[CYXTwoViewController alloc]init]; [self setuponechildviewcontroller:twovc image:[uiimage imagenamed:@ "JS"] title : @ "Technology"]; // 3. Add a 3rd controller     CYXTHREEVIEWCONTROLLER *THREEVC = [[cyxthreeviewcontroller alloc]init]; [self setuponechildviewcontroller:threevc image:[uiimage imagenamed:@ "QW"] title:@ "blog"]; // 4. Adding a 4th controller cyxfourviewcontroller *fourvc = [[ Cyxfourviewcontroller alloc]init]; [self setuponechildviewcontroller:fourvc image:[uiimage imagenamed:@ "User"] title:@ "My Lake";} /** * method of adding a sub-controller */- (void) Setuponechildviewcontroller: (uiviewcontroller *) Viewcontroller image: (uiimage *) Image title: (nsstring *) title{ Uinavigationcontroller *navc = [[uinavigationcontroller alloc]initwithrootviewcontroller: viewcontroller]; navc.title = title; navc.tabbaritem.image = image; [navc.navigationbar setbackgroundimage:[ uiimage imagenamed:@ "COMMENTARY_NUM_BG"] forbarmetrics:uibarmetricsdefault]; viewController.navigationItem.title = title; [self ADDCHILDVIEWCONTROLLER:NAVC];}
Transferred from: http://www.cocoachina.com/cms/wap.php?action=article&id=13351
10 minutes to build App mainstream framework