IOS 11 Development Tutorials (vii) write the first iOS11 code Hello,world
Code is a collection of command sequences that are written in a computer language to implement a particular function. Now it's time to implement the function of displaying the "Hello,world" string in the text box by code, as follows:
(1) Use the three view mode icon in the Settings editor, 1.25 to adjust the Xcode interface to the effect shown in 1.26.
Figure 1.25 An icon for the three view mode of the editor
Figure 1.26 Adjusting the interface
(2) Ctrl + Drag the text box object in the main view, a blue line appears, and the blue line is dragged to the Viewcontroller.swift file, as shown in 1.27.
Figure 1.27 a blue line appears
(3) After releasing the mouse, a dialog box will pop up, showing 1.28.
Figure 1.28 Popup dialog box
(4) in the Pop-up dialog box, find the name of this entry, which is shown in tf,1.29.
Figure 1.29 Entering a name
Note: The name of this entry is arbitrary.
(5) Select the Connect button to close the dialog box, and automatically generate a line of code in the Viewcontroller.swift file, as shown in 1.30.
Figure 1.30 Socket variables
Note: The generated code is called a socket variable, and the socket variable is actually an alias for the associated object. The developer can manipulate this socket variable to manipulate the associated object. This is done in conjunction with the socket variable declaration and the association, and there is a way to first declare an action after the association. The following are the steps:
First, open the Viewcontroller.swift file and use the Iboutlet keyword to declare the socket variable for the text box, as shown in code 1.31.
Figure 1.31 declaring the socket variable
Note: A well-declared socket variable will appear in front of the code with a small, hollow circle. This small circle indicates that the socket variable has not yet been associated.
Next, use the three-view icon in the Settings editor to adjust the Xcode interface to the same effect as in figure 1.26.
Then, holding down the CTRL key while dragging the text box object in the main view, a blue line appears, associating the blue line with the socket variable in the file Viewcontroller.swift file, as shown in 1.32.
Figure 1.32 Associative socket variables
Finally, when the mouse is released, the text box object is associated successfully with the socket variable, and the hollow small circle in front of the socket variable becomes a small, solid circle that indicates that the socket variable is already associated.
(6) Open the Viewcontroller.swift file and write the code that implements the function of displaying the string Hello,world in the text box. The code is as follows:
Import UIKit
Class Viewcontroller:uiviewcontroller {
@IBOutlet weak var tf:uitextfield!
Override Func Viewdidload () {
Super.viewdidload ()
Additional setup after loading the view, typically from a nib.
tf.text= "Hello,world" //Set text content
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
Dispose of any resources the can be recreated.
}
}
Note: To make it easier for developers to understand the code, this book will omit the extra code and use the "..." ellipsis to indicate that the above code will change to the following code:
Import UIKit
Class Viewcontroller:uiviewcontroller {
@IBOutlet weak var tf:uitextfield!
Override Func Viewdidload () {
Super.viewdidload ()
Additional setup after loading the view, typically from a nib.
tf.text= "Hello,world" //Set text content
}
......
}
When you run the program, you see the effect shown in 1.33.
Figure 1.33 Running effect
IOS 11 Development Tutorials (vii) write the first iOS11 code Hello,world