[Fall in love with Swift] third bomb: Using Swift to build the basic foundation of apps

Source: Internet
Author: User

sticked

First of all, we'll use iOS's own navigation controller to build a simple foundation for an app,

Create a new empty application and 3 Swift files, named: Firstviewcontroller,secondviewcontroller,thirdviewcontroller;

Also rewrite the Viewdidload () method of the inheriting class in three Swift controllers:

Override func viewdidload () {        super.viewdidload ();}

The Appdelegate.swift file in the whole program is the event proxy class of the whole app, which can show the life characteristics of the whole app, as well as the function of the globle file in the. NET program, which can be used as the main program declaration.

In this file we add the following code to register the 3 controllers created in application:

Func application (application:uiapplication, Didfinishlaunchingwithoptions launchoptions:nsdictionary?)Bool {Self.window=UIWindow (Frame:UIScreen.mainScreen (). Bounds)//Override point for customization after application launch.self.window!. BackgroundColor =Uicolor.whitecolor () let FIRSTVC=Firstcontroller (); Let Nav1=Uinavigationcontroller (ROOTVIEWCONTROLLER:FIRSTVC); Let Image1=uiimage (named:"H.png"); Nav1.tabbaritem=uitabbaritem (title:"Finished Food ingredients", Image:image1,tag:1); Let SECONDVC=Secondconreoller (); Let Nav2=Uinavigationcontroller (ROOTVIEWCONTROLLER:SECONDVC); Let Image2=uiimage (named:"O.png"); Nav2.tabbaritem=uitabbaritem (title:"Guosetianxiang", Image:image2,tag:2); Let THIRDVC=Thirdcontroller (); Let Nav3=Uinavigationcontroller (ROOTVIEWCONTROLLER:THIRDVC); Let Image3=uiimage (named:"S.png"); Nav3.tabbaritem=uitabbaritem (title:"Affordable Classic", Image:image3,tag:3); Let Navarr=[nav1,nav2,nav3]; Let Tabbarcontroller=Uitabbarcontroller (); Tabbarcontroller.viewcontrollers=Navarr; Self.window!. Rootviewcontroller=Tabbarcontroller; Self.window!. makekeyandvisible ()return true    }

Brief description:

By defining 3 variables, we declare the 3 controllers we created, by setting the Rootviewcontroller of the Uinavigationcontroller, Load 3 controllers in 3 Uinavigationcontroller.

We also decided to put 3 more navigationcontroller in another container: Uitabbarviewcontroller,

Such as:

        Let navarr=[nav1,nav2,nav3];        Let Tabbarcontroller=uitabbarcontroller ();        Tabbarcontroller.viewcontrollers=Navarr;        Self.window!. Rootviewcontroller=tabbarcontroller;

As we also need to tabbar on the friendly display of the corresponding navigation information, so we set the corresponding Uinavigationcontroller with a picture with the Tabbaritem:

        Let Image1=uiimage (named:"h.png");        Nav1.tabbaritem=uitabbaritem (title:" finish food material ", Image:image1,tag:1 );

The program runs up and looks at the effect:

The effect of not adding tabbaritem pictures:

After setting the Tabbaritem effect:

(Because did not go online to find the corresponding picture, so casually used some of the previous pictures will be ha.) )

Although we just moved the appdelegate file, a common shelf was present in front of us,

A simple built-in controller container with iOS comes up with a common app shelf, which is really good compared to the other two mobile platforms Android and Wp,ios, because in front of moving this small form, our operation is limited, prefabricated this 20 The percent function reduces things by 80%, and does not look as broken as Android.

Padding content:

Now that the shelves are up, it's time to fill in the contents with the corresponding controller.

We want users to see the changes of the corresponding 3 created controllers when they click on the 3 icons of the tabbar below.

So we implement TableView in each controller:

class Thirdcontroller:uiviewcontroller,uitableviewdelegate,uitableviewdatasource

First, according to international practice, we inherit uitableviewdelegate and Uitabledatasource for 3 self-built controllers.

Friends who are familiar with. NET See DataSource will think that before WebForm or WinForm use gride will define the corresponding datasource, so just remember, when we want to render and use gride, we need to inherit datasource, after inheriting the two agents You have to implement two methods:

    //two methods in a delegate that implements inheritanceFunc TableView (tableview:uitableview!, numberofrowsinsection section:int)int{return  -; }        //Return to CellFunc TableView (tableview:uitableview!, Cellforrowatindexpath indexpath:nsindexpath!) ->uitableviewcell!{Let cellid="Cell ID"; varCell=tableview.dequeuereusablecellwithidentifier (Cellid) as?UITableViewCell; returncell; }

The first is the number of rows (INT) that returns table;

The second one is to return each cell in the table;

Next, we simulate populating an array to render the TableView:

    //prefabricated Data   vardatalist=Nsmutablearray (); //TableView container for placing and displaying data    var_tableview:uitableview?; Overridefunc viewdidload () {super.viewdidload (); Self.title="Affordable Classic"                 for(varI=0;i< -; i++) {Datalist.addobject ("first \ (i) Benefits"); }        //get the boundaries of the current view        varrect:cgrect=Self.view.bounds; _tableview=UITableView (frame:rect,style:.        Plain); _tableview!.Delegate=Self ; _tableview!. Datasource=Self ;    Self.view.addSubview (_tableview); }        //two methods in a delegate that implements inheritanceFunc TableView (tableview:uitableview!, numberofrowsinsection section:int)int{returnDatalist.count; }        //Return to CellFunc TableView (tableview:uitableview!, Cellforrowatindexpath indexpath:nsindexpath!) ->uitableviewcell!{Let cellid="Cell ID"; varCell=tableview.dequeuereusablecellwithidentifier (Cellid) as?UITableViewCell; if(Cell = =Nil) {Cell=UITableViewCell (style:.        Default,reuseidentifier:cellid); }                varStr=datalist.objectatindex (Indexpath.row) as?String; Cell!. textlabel.text=str; returncell; }

Rendering results:

This way we will see the different controllers being presented by clicking on different Tabbar.

We would like to see how to get TableView corresponding to the click line, how to do?

    // Point Row Event    Func TableView (Tableview:uitableview!,didselectrowatindexpath indexpath:nsindexpath!) {    println (" indexpath.row" line was clicked );    }

This allows you to see the output after clicking on different rows through the console.

directory structure, creating an MVC bright through folders:

The entire simple iOS-based container controller has emerged, combined with the convenience of iOS, plus the readability of Swift makes iOS development a lot easier than before.

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.