A probe into Swift UI development

Source: Internet
Author: User
Tags uikit

Apple just released the swift programming language early this morning, and Swift is the new programming language for iOS and OS X applications. I believe many developers are learning the new language.
No more nonsense, I'm going to learn to use Swift to create a simple UI application.

For Swift syntax, refer to the Apple Swift programming language Getting Started Tutorial

The effect is as follows:

Development environment
    • Xcode6-beta
    • IOS8
Create a project
    • Choose File > New > Project > (IOS or OS X) > Application > Your template of choice.

      Select single view application here.

    • Click The Language pop-up menu and choose Swift.

Adding basic controls

Encoded in the Viewcontroller.swift file, which is similar to the Objective-c viewcontroller.m.

    • UILabel

UILabel controls are often used to display text labels

Let's create a label that looks at the Uilabel class and discovers that it inherits from UIView, Nscoding.

You can set the size and Lebel text by using a method like create view, and add it to the current view by Addsubview method.

The code is as follows:

        Let label = UILabel (Frame:cgrect (Origin:cgpointmake (10.0, 50.0), Size:cgsizemake (150,50)))//let is a keyword        for Swift to represent constants Label.text = "This is a label"        Self.view.addSubview (label)

Uilabel created parameters use aliases, which are like object-c.

    • UIButton

UIButton controls are often used for buttons.

Let's create a button and set its handling of the Uicontrolevents.touchupinside event, and view the UIButton class to discover that it inherits from Uicontrol, Nscoding.

You can create a button with a method like create view, specify the location and size, then set the button's Titile, set the button's background color, and set the button's Touch event.

Finally, it is added to the current view by the Addsubview method.

The code is as follows:

        Let btn = UIButton (Frame:cgrect (Origin:cgpointmake (10.0, 110.0), Size:cgsizemake (150,50)))        btn.settitle ("button ", forState:UIControlState.Normal)        Btn.backgroundcolor = Uicolor.redcolor ()        btn.addtarget (Self, Action:" ButtonClick: ", ForControlEvents:UIControlEvents.TouchUpInside)        Self.view.addSubview (BTN)

The ButtonClick method is implemented as follows:

    Func ButtonClick (sender:uibutton!) {    }

Behind the UIButton! This means that sender can be any subclass inherited by UIButton.

    • Uialertview

Uialertview is often used for pop-up dialogs, let's create an alert.

Uialertview class inherits from UIView, we first create an alert and then set the alert's title, message, button, delegate.

Then call Uialertview's Show method, which displays alert.

We are dealing with the creation and display of alert in the touch callback event of the button. Add the following code to the ButtonClick method:

        var alert = Uialertview ()                //directly create a bug        //var alert = Uialertview (title: "Alert", Message: "This is an alert", dele Gate:self, Cancelbuttontitle: "Cancel")        Alert.title = "alert"        alert.delegate =        Self Alert.addbuttonwithtitle ("Cancel")        Alert.message = "This was an alert"                alert.show ()

Delegate and self, still have object-c shadow.


Modify Viewcontroller's declaration and join Uialertviewdelegate

Class Viewcontroller:uiviewcontroller, Uialertviewdelegate

Implement the delegate method of alert to handle the Click event of a button.

    button for handling alert Click    Func alertview (alertview:uialertview!, Clickedbuttonatindex buttonindex:int) {        println ("Buttonindex:\ (Buttonindex)")    }
Summarize

Swift's Uikit API interface and Objective-c's API interface are generally consistent and familiar with the original Uikit interface, and it should be fast to get started with swift UI development.

The documentation and API manuals allow you to see how each OBJECTIVE-C API is programmed using Swift's API.

You can get a demo of this article here.

A probe into Swift UI development

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.