Game Development Reference
In-Depth Information
UIApplicationDelegate
, which
AppDelegate
implements, defines the tasks that will be called on the
application's behalf.
If we take a look at the implementation of
application:didFinishLaunchingWithOptions:
, we
start by creating a new
UIWindow
with a size equal to the size of the screen. Once the
UIWindow
is
instantiated, we need to create a
UIViewController
that will manage the UI of our application. A
new Xcode project, like the one we created, starts out with a single
ViewController
class to manage
our UI. We are going to create device-specific subclasses of our
ViewController
, so that we have a
place to put device-specific code. We will look at the process of creating these subclasses shortly.
To make sure that we instantiate the correct ViewController subclass, we need to add the bold code
in Listing 2-2.
Once our
ViewController
is created, we set it as the
rootViewController
of
window
and call
. The window object is an instance of
UIWindow
and is the root graphical
makeKeyAndVisible
essentially displays the window. Any
window
object.
makeKeyAndVisible
. This might include reading an app-specific
iOS development and the associated libraries make heavy use of the Model View Controller (MVC)
pattern. In general, MVC is a strategy for separating the presentation (View), data (Model), and
business logic (Controller). In specific terms, the model is simply data, like a
Person
class or an
Address
. The view is responsible for rendering the data to the screen. In iOS development, that
means a subclass of
UIView
. iOS provides a special class to act as the controller for a
UIView
, which
is aptly named
UIViewController
.
UIViewController
has two key characteristics: it is often associated with an XIB file, and it has
a property called “view” that is of type
UIView
. By creating a subclass of
UIViewController
, we
can also make an XIB file with the same name as the class. By default, when you instantiate a
UIViewController
subclass, it will load a XIB with the same name. The root
UIView
in the XIB will be
wired up as the
view
property of
UIViewController
.
Besides providing a clean separation between the UI layout and the logic that drives it, iOS provides
a number of
UIViewController
subclasses that expect to work with other
UIViewControllers
instead of
UIViews
. An example of this is the
UINavigationController
that implements the type of
navigation found in the Settings app. In code, when you want to advance to the next view, you pass
a
UIViewController
and not a
UIView
, though it is the
view
property of the
UIViewController
that is
displayed on the screen.
Admittedly, for our example application in this chapter, it does not make a lot of difference if we use
a
UIViewController
. In Chapter 1, we extended
UIView
when we created the
RockPaperScissorsView
class and it worked fine. However, understanding how
UIViewControllers
and their views work
together will make life easier in Chapter 3, where we explore a game's application life cycle.