With controls in place, connect up outlets and actions in Xcode. Learn the major differences between iOS and watchOS actions, and how to name Outlets and actions following Swift 3.0 style guidelines. Learn how to remove an outlet and clean a project.
- [Instructor] With the interface laid out it's time to connect the outlets to the interface controller. Use the assistant editor to do this. Close up all your other windows and open the assistant editor to give yourself some space. You might also have to drag or hit the minus button to zoom down a little bit so you can see everything. In this application we won't need any of the template code so we'll be getting rid of that. If you can drag down and collect all that you can hit delete, giving yourself a nice clean interface controller.
Select the label and control drag to the interface controller and release. You get an outlet and it asks for a name which we're going to call label. Connect the label. Now do the same thing with the pizza. Control drag from pizza to your code and call this pizza, and connect that. Actions are just as easy. Again with pizza, control drag from pizza to the code and type in pizza and select action.
You'll notice action doesn't have as many options as outlet does, you can just type the name. Hit connect. Notice we get a special error that says invalid redeclaration of pizza. This is a quirky thing about swift and watch OS. The action pizza with empty parentheses is the same identifier as the outlet pizza with no parentheses. When using outlets and actions use a convention Apple has championed in the swift 3 style guide.
The simple rule is to make the outlet's identifier a noun such as pizza or hello label. For actions, use a verb or predicate phrase such as button pressed, switched change, or did press pizza. Especially in watch OS use this convention of actions as verb or verb phrases and outlets as nouns. To correct this error delete the pizza outlet since we won't need it. However you'll need to do several steps to delete or change a connection. First right click on the pizza button to disconnect it by pressing the x.
We'll disconnect both outlets, then change your code. Comment out or delete the pizza outlet. Rename the action did press pizza. Next clean the project by hitting command+shift+k or going to the menu and selecting product, clean. Finally, connect the action of the button again by dragging from the circle to the control. As I mentioned earlier you can connect from the document outline too.
Control drag from the ice cream button in the outline in the code and make an option named did press ice cream. Close the assistant editor and navigate to the interface controller.swift code in the extension. You'll see we have our two actions and out outlets. These are our completed connections. Remember this big difference between iOS and watch OS. Many actions do not have parameters like sender in iOS.
Be sure to use names that better describe if something is an action or an outlet in watch OS so there are no identifier conflicts.
- Breaking down the parts of a watchOS app
- Building an interface
- Adding and scaling images
- Adding navigation
- Playing audio and video
- Connecting objects to code
- Writing watchOS code
- Working with tables
- Programming notifications
- Working with complications