For this app, our setup process will closely resemble that of the previous chapter.
To create the Xcode project, follow the given instructions:
The project window should now be looking similar to this:
Looking at the project navigator on the left, we see that inside the C-Quence project is a folder also named C-Quence, which contains files that are relevant to the iPhone companion app that we will not be needing to change in this chapter. The next two folders are all about the WatchKit Extension and WatchKit App. We can see here that Xcode's default project structure adheres very closely to the MVC pattern described above.
The MVC's View is the .storyboard
file that the template creates for us. Similarly, the Controller is already created, namely, the InterfaceController.swift
file.
The only part of the pattern that we need to create from scratch is the Model, which is out next step:
GameLogic.swift
, and be sure to check that the Group it is assigned to is the C-Quence WatchKit Extension and that the target selected is also C-Quence WatchKit Extension (since you selected the extension group in the Navigator before creating the file, these should be correctly preselected):Capitalization is not a matter of taste.
There are certain conventions around when to use capitals and when not and these conventions can be critical in making your code easier to understand, either for other developers or for yourself later on.
Class names are always capitalized, as in the case of the GameLogic
class we will create in a moment. Using further capitals for complete words in the name, such as the Logic in GameLogic
is called camel case naming and is a great aid to clarity.
As we see above with GameLogic.swift
, file names are also capitalized (but not the file type suffix!).
Variable names are written in uncapitalized camel case, for example myVariableName
; this includes everything that is declared with the var
and let
keywords, as well as function argument names.
We now have all the files needed for a simple but well-structured app project that follows the Model-View-Controller program design pattern. It is a good idea to have some structure like this, however imperfect and incomplete it may be, before we start to dive into the coding and immerse ourselves in detail. Anything that we have missed, or that turns out quite differently to the way we expect, we can add and alter as we go along. Remember, a plan that may need revising is way better than no plan at all.
The interface we will be building here makes use of the 'bordered' buttons we created in the previous chapter.
We need four such buttons, and we will use a hierarchy of Group objects to lay them out. Most of this layout method falls into the drag-drop-set-value-repeat category, but that does mean we'll be glad of the clear outlines we have already set out earlier.
In the project Navigator, select the Interface.storyboard
file from the C-Quence WatchKit App group, and if the Attributes Inspector pane is not open, click Command-Option-4.
This is how the Group hierarchy that we are aiming to construct will be organized:
But it doesn't look that way yet:
We will drag a Group object into the Interface Controller, to be named Gameboard Group, that will contain all the buttons and groups visible during game play. Inside that we will organize two groups, the Upper and Lower Row groups, each of which will contain two color groups side by side. A button (not shown in the image above) will be placed in each color group.
For setting up the Group hierarchy, follow the given steps:
Finally, drag a Button into the Red Group, erase its Title, since we have no need of a text title, and set its Background Color to Black Color.
This is where the Document Outline really shines. Select the Red Group.
Your Document Outline and interface should now look like these:
Select Gameboard Group in the Document Outline and set its View | Hidden setting (below the Alpha setting) to selected (ticked) in the Attributes Inspector.
It promptly disappears from the interface, so why did we do that? The game's interface is actually very simple, either we are looking at the gameboard that we have just built, or a play button (and possibly results label), and, rather than create different views, it is much simpler to hide whichever of the two 'views' that we don't need. At the launch of the app, we want to see the play button, but not the gameboard. So we hide it.
Setting the hidden property to true
conveniently allows us to drag UI objects from the library onto (what is now) an empty view in Interface Builder, so let's do that now. Drag a Label object into the interface, and name it Result Label. Set its Text Alignment to Center and the Lines property to 0. This will allow the label to show as many lines of text as is necessary, in case we decide to present something like "Hey, awesome, you just scored an amazing score of 128" when we show the user his result.
Set both Horizontal and Vertical Alignment to Center, and Width to Relative To Container. We'll set View | Hidden to true
, since we don't need the result label when the app first launches. Now drag a Button into the (once again empty) interface, change its Title to Play, and set both Horizontal and Vertical Alignment to Center.
You're done! The interface is now complete. You could hit Run, but there won't be much to see apart from a Play button that doesn't do anything. First we need to create a game logic class and populate the InterfaceController.swift
class with some code. Since the InterfaceController.swift
code will need to call methods of the game logic class, we will implement the latter first. You'll see that the game's actual logic is really very simple, and this class will comprise little more than two dozen lines of code.
18.219.99.28