AppleWatch Development Tutorial Watch Application Object new introduction and writing run code add Watch app object when new content introduced
When the Watch app object is added to the created project, it contains two parts: Watch app and WatchKit extension,2.18. Where the Watch app is part of the user's iwatch, it is so far only allowed to contain storyboard files and resources files. In our project, this section does not include any code. The WatchKit extension section is located on the user's iphone-installed app, which includes the code logic and other resource files we need to implement.
Figure 2.18 New section
The two parts are connected via WatchKit, and the iPhone is matched with watch after the user clicks The watch App . will start WatchKit Extension , and then Watch establish the connection, generate the communication,as shown in 2.19.
Figure 2.19 Communication
Running the Apple Watch program
Once you have added the Watch app to the created project, you can run the iwatch program. Running the program is actually very simple, developers just need to click the Run button can be run, as shown in 2.20. Compile the program first before running the program, and if the program is correct, a build succeeded string will appear, as shown in 2.21. If there is an error in the program, a build failed string is displayed, as shown in 2.22.
Figure 2.20 Running the button
Figure 2.21 Editing success Figure 2.22 compilation failed
After the program is compiled, programs are automatically connected, run, and run with effects 2.23 and 2.24.
Figure 2.23 Run effect figure 2.24 Run effect
Note: figure 2.23 is the iwatch simulator, the Apple Watch simulator, that is used to visualize the iwatch application. Because the program is not written and the editing interface is not set, there is no effect at this point. Figure 2.24 is the iOS emulator, which is associated with the iwatch emulator. This is usually the case when you run the iwatch application.
If the iwatch simulator does not appear, there are two workarounds available here:
1. Set the running target
Set the run target (set the active scheme) as shown in Hello WatchKit app,2.25.
Figure 2.25 Setting the run target
2. Setting up the hardware
Select the iOS emulator and select hardware| in the menu that appears External displays| Apple watch-38mm or a 42mm command, shown in 2.26.
Figure 2.26 Setting up the hardware
The editing interface for Apple watch programming
The edit interface (Interface Builder) is mentioned in section 2.4.4. The editing interface is used to design the user interface, and clicking the open Interface.storyboard file opens the editing interface. In Xcode 6.2, the editing interface directly uses a storyboard. This section provides an introduction to the editing interface
1. Introduction to the interface
When you click Interface.storyboard to open the editing interface, you can see that the editing interface is made up of 4 parts, as shown in 2.27.
Figure 2.27 Composition of the interface
Of these, the four parts are described as follows:
The part with Q number 1 is the navigation window.
The part with Q number 2 is the canvas: where you design the user interface, the area in the canvas where the arrows point is the interface, and you can have multiple interfaces in the canvas.
The section Q number 3 is the inspector for the component window: Used to edit the properties of the currently selected object.
The part of Q number 4 is the Library of the component window: If you choose objects, you have a lot of objects stored inside. There is a small dock above the canvas's interface, which is an abbreviated version of the File manager.
Note: The first level of controls in the dock display interface, with at least one interface Controller per scene
2. Design interface
It was mentioned in 2.4.4 that the editing interface was not designed so that there was nothing in the running effect. This section will display a label on the iwatch emulator. The following steps are described:
(1) Open the Interface.storyboard file in the Hello project.
(2) Locate the Label object from the object library, drag it to the interface, and 2.28.
Figure 2.28 Adding a label
When you run the program, you see the effect shown in 2.29.
Figure 2.29 Running effect
There are two points to note when designing the interface:
(1) Design interface Dimensions
In Figure 2.26, we can see that the iwatch simulator has two sizes, one 38mm is 42mm, the interface of the corresponding editing interface may also be set to 38mm or 42mm, set the interface size of the specific operation steps are as follows:
First, open the Interface.storyboard file, as shown in 2.30.
Figure 2.30 Setting the size 1
Then, select the Any screen size option, and a dialog box appears, as shown in 2.31.
Figure 2.31 Setting the size 2
Finally, selecting any size in this dialog box allows you to set the interface size.
(2) setting the style of the object
The developer can set the color, font, etc. of the label in the Open show The Attributes inspector, which is the property Viewer panel.
Writing code for Apple Watch
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 "Hello,world" string in the Label object by code, as follows:
(1) Use the three view mode icon in the Settings editor, 2.32 to adjust the Xcode interface to the effect shown in 2.33.
Figure 2.32 An icon for the three view mode of the editor
Figure 2.33 Operation Step 1
(2) Holding down the CTRL key to drag the label object in the interface, a blue line will appear, drag the Blue line to the Interfacecontroller.swift file under the Hello WatchKit extension folder, as shown in 2.34.
Figure 2.34 Operation Step 2
(3) after releasing the mouse, a dialog box with the Declaration and the associated socket variable will pop up, as shown in 2.35.
Figure 2.35 Operation Step 3
(4) set the name to label,2.36 as shown.
Figure 2.36 Operation Step 4
Note: The name here can be arbitrary.
(5) Click the Connect button to see the code shown in the Interfacecontroller.swift 2.37.
Figure 2.37 the generated code
Note: The generated code is called the socket variable, and the socket variable is actually an alias for the associated object, and the developer can manipulate the socket variable to manipulate the associated object.
(6) Open the Interfacecontroller.swift file and write code that implements the function of changing the content displayed in the label into a Hello,world string. The code is as follows:
- Import WatchKit
- Import Foundation
- Class Interfacecontroller:wkinterfacecontroller {
- @IBOutlet weak var label:wkinterfacelabel!
- Override func Awakewithcontext (Context:anyobject?) {
- Super.awakewithcontext (context)
- Configure interface objects here.
- label.settext ("Hello,world")
- }
- ...//This omits the method that is called when the Watch controller is about to be visible and is no longer visible.
- }
When you run the program, you see the effect shown in 2.38.
Figure 2.38 Running effect
This article is selected from: Apple Watch Development Quick Start Tutorial University PA information, reproduced Please indicate the source, respect the technology respect the IT person!
AppleWatch Development Tutorial Watch Application Object new introduction and writing run code