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