IOS 9 App development tutorial editing interface and writing code

Source: Internet
Author: User

IOS 9 Application Development tutorial editing interface and writing code editing interface

The editing interface (Interface builder) is mentioned in the 1.2.2 section, and the editing interface is designed to design the user interface, which opens the editing interface by clicking Open Main.storyboard file. After Xcode 5.0, the editing interface directly uses a storyboard. This section provides an introduction to the editing interface

1. Composition of the interface

When you click Main.storyboard to open the editing interface, you can see that the editing interface is made up of 4 parts, as shown in 1.23.

    • The section numbered 1 is the dock.
    • The part with the number 2 is the canvas: where the user interface is designed, the area that the arrows point to in the canvas is the design interface, which can have multiple design interfaces in the canvas, typically referred to as a scene or a main view of the design interface.
    • The section numbered 3 is the Inspector for the tool pane: Used to edit the properties of the currently selected object.
    • The section numbered 4 is the library for the tool pane: If you choose objects, you have a lot of views in it.

Figure 1.23 Editing Interface composition

    • There is a small dock below the canvas's design interface, which is a reduced version of a file manager. The dock shows the first-level controls in the scene, with at least one viewcontroller, one firstreponder, and one exit per scene. But there are other controls available, and the dock is also used for simple connection controls, as shown in 1.24.

Figure 1.24 Dock

2. Set the main view size

In Figure 1.23, you can see that the main view is not the size of the phone, which is actually adjustable. To make it easier and more accurate for developers to design the phone interface, we can adjust the size of their view to fit. Here's how to resize the main view to the size of your iphone 6 phone, in the following steps:

(1) Select View Controller in the dock above the

(2) in the inspector on the right side of the tool pane, select Show the Attributes inspector as the property inspector, and in the Property Inspector panel that appears, set the size as shown in iphone 4.7-inch,1.25.

Figure 1.25 Setting the dimensions of the design interface

Note: In the Property Inspector panel, you can set the orientation, status bar, and so on, in addition to the dimensions of the main view.

After you set the size, the canvas is shown in effect 1.26.

Figure 1.26 Effect of the canvas

3. Add a View object

If you want to display a text box on the iOS emulator, add an object to the main view. Click the Show the Object Library window in the tool pane library, where you find the Text field textbox object and drag it to the main view of the canvas, as shown in 1.27

Figure 1.27 Adding a View object

When you run the program, you see the effect shown in 1.28. Tapping the text box in the simulator will appear on the keyboard, and you can use the keyboard to implement the input of the string, as shown in 1.29.

Figure 1.28 Run effect figure 1.29 Run effect

Writing iOS9 Code

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.30 to adjust the Xcode interface to the effect shown in 1.31.

Figure 1.30 An icon for the three view mode of the editor

Figure 1.31 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.32.

Figure 1.32 a blue line appears

(3) After releasing the mouse, a dialog box will pop up, showing 1.33.

Figure 1.33 Popup dialog box

(4) in the Pop-up dialog box, find the name of this entry, which is shown in tf,1.34.

Figure 1.34 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.35.

Figure 1.35 Action 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.36.

Figure 1.36 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.31.

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

Figure 1.37 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.38.

Figure 1.38 Running effect

This article is selected from:IOS 9 Application Development Basic Tutorial University bully internal information, reproduced please indicate the source, respect the technology respect the IT person!

IOS 9 App development tutorial editing interface and writing code

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.