10 minutes to build App mainstream framework

Source: Internet
Author: User
Tags set background

Build a mainstream framework interface

    • 0. Achieving results

      • 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

    • 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)

Build a mainstream framework (pure code) starting from 0

1. Preparatory work

    • Environment deployment

2. 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

    • 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. 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

Related Article

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.