Building an App Mainstream framework _ code building (OC)

Source: Internet
Author: User

Reprinted from: Http://my.oschina.net/hejunbinlan/blog/529778?fromerr=EmSuX7PR Building the mainstream framework interface
    • Source address at the end of the article
    • Achieve results

      • Note: This part of the icon and from [it lake] Https://github.com/itjhDev/itjh
Guide
    • 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
    • We mainly discuss the construction of the main frame, the data is not added temporarily
Analyze the basic process of doing the project
    • 1. Building the main framework of the project
      • (1) Build Tabbarcontroller first (there is a bar below)
      • (2) Re-build the Navigationcontroller (there is one, and each sub-controller is not the same)
    • 2. Thinking about how to develop
      • (1) Storyboard Construction (use when the interface is very small)
      • (2) Pure Code building (more than 5 interfaces when used, easy to manage, commercial projects, generally use this way)
Starting from 0 to build the mainstream framework (pure Code) 1. Preparatory work
    • Environment deployment

Snip20150904_11.png2. Initial setup of the basic interface
    • First Step design catalog (based on modular +MVC idea, create basic file directories and files)
      • Modular thinking Create a directory path (typically created under real-world paths, then dragged into the project)
      • Custom Tabbarcontroller

Snip20150904_4.png
    • The second step on the code (set Windows within APPDELEGATE.M to start the root controller)
-(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;}
    • Step three, create and add a sub-controller within CYXTABBARCONTROLLER.M
-(void) viewdidload {[Super viewdidload];//1. Add first controller//1.1 Initialize Cyxoneviewcontroller *ONEVC = [Cyxoneviewcontrolle R Alloc]init]; 1.2 Add ONEVC as Uinavigationcontroller root controller Uinavigationcontroller *nav1 = [[Uinavigationcontroller alloc] INITWITHROOTVIEWCONTROLLER:ONEVC];    Set Tabbar title Nav1.title = @ "Home"; [Nav1.navigationbar setbackgroundimage:[uiimage imagenamed:@ "COMMENTARY_NUM_BG"] forbarmetrics: Uibarmetricsdefault]; Set Tabbar icon Nav1.tabBarItem.image = [UIImage imagenamed:@ "Tab_home_icon"]; Set Navigationbar title OneVC.navigationItem.title = @ "Home"; Set the background color (these actions can be given to each individual sub-controller to do) OneVC.view.backgroundColor = [Uicolor Whitecolor]; 1.3 Give Uinavigationcontroller to Uitabbarcontroller management [self addchildviewcontroller:nav1]; 2. Add 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. Add a 3rd controller Cyxthreeviewcontroller *THREEVC = [[Cyxthreeviewcontroller alloc]init];    Uinavigationcontroller *nav3 = [[Uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:THREEVC];    Nav3.title = @ "blog post";    Nav3.tabBarItem.image = [UIImage imagenamed:@ "QW"];    ThreeVC.navigationItem.title = @ "blog post";    ThreeVC.view.backgroundColor = [Uicolor Yellowcolor]; [Self addchildviewcontroller:nav3]; 4. Add 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

Snip20150904_8.png
    • 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 sub-controller methods */-(void) setupallchildviewcontroller{//1. Add the first controller Cyxoneviewcontroller *ONEVC = [[Cyxoneviewcontroll    ER 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. Add 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];}
    • Attached: source GitHub address

Building an App Mainstream framework _ code building (OC)

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.