Swift UI development

Source: Internet
Author: User

Address: http://www.tairan.com/archives/6600

For more information about Swift syntax, see 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.

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

Add basic controls

Encode in the ViewController. swift file, which is similar to ViewController. m of Objective-C.

  • UILabel

The UILabel control is used to display text labels.

Next we will create a label to view the UILabel class and find that it inherits from UIView and NSCoding.

You can set the size and lebel text by using a method similar to creating a view, and add it to the current view through the addSubview method.

The Code is as follows:

Let label = UILabel (frame: CGRect (origin: CGPointMake (10.0, 50.0), size: CGSizeMake (, 50) // let is the key word label of the constant represented by Swift. text = "This is a Label" self. view. addSubview (label)

 

UILabel creates a parameter using an alias, which is similar to Object-C.

  • UIButton

The UIButton control is commonly used for buttons.

Next we will create a button and set its UIControlEvents. TouchUpInside event processing. Check that the UIButton class inherits from UIControl and NSCoding.

You can create a button by specifying the position and size, setting the titile of the button, setting the background color of the button, and setting the touch event of the button.

Finally, add it to the current view through 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!){}

 

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

  • UIAlertView

UIAlertView is often used in pop-up dialog boxes. Next we will create an alert.

The UIAlertView class inherits from UIView. We first create an alert and then set the title, message, button, and delegate of alert.

Call the show method of UIAlertView to Display alert.

We process 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", delegate: self, cancelButtonTitle: "cancel") alert. title = "alert" alert. delegate = self alert. addButtonWithTitle ("cancel") alert. message = "this is an alert" alert. show ()

 

Delegate and self still have the shadow of Object-C.

Modify the ViewController declaration and add UIAlertViewDelegate.

classViewController:UIViewController,UIAlertViewDelegate

 

Implement the alert delegate method to process the button click event.

// Process the alert button click func alertView (alertView: UIAlertView !, ClickedButtonAtIndex buttonIndex: Int) {println ("buttonIndex: \ (buttonIndex )")}

 

Summary

The Swift UIKit API interface is consistent with the Objective-c api interface in general. If you are familiar with the original UIKit interface, Swift UI development should be quick.

You can use the document and API manual to view how each Objective-c api uses the Swift API for programming.

You can obtain the Demo of this article here.

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.