After we have added a navigation controller in part 4 of the iPhone tutorial, we can now begin to add
Views to the app.
Views are the screens of the app. A
View is usually accompanied by a
ViewController which is responsible to manage the
View and all the action that is supposed to go on, like reacting on user events.
UINavigationController that we added in the last part is responsible for managing all the
ViewControllers of our app. Because of this, we shouldn’t add anything to the
Window directly. Everything we want to show goes into the
UINavigationController. So, whenever we want to add a new screen to the app, we add a
UIViewController to the
UINavigationController. The root view controller (the one which is displayed initially) and its View can be added conveniently in Interface Builder:
Also add a
Slider, Label and
Rounded Rect Button to the View and set a title for the ViewController so we have some controls to experiment with later on:
So by now, if you run the app, you should see the controls you just added inside the
View which is managed by the
ViewController which is managed by the
Now, let’s add a some behaviour to our new view. This is the point where we need to start to work with the programming language
In this tutorial, I’ll take a chance to try to explain everything to the point where somebody who never did any programming before has a chance to follow. If you already know other programming languages, skip the
(for non-programmers) sections and read the
(for programmers) sections instead (and vice-versa).
We have already seen ‘Objects’ and created them using Interface Builder. Things like Buttons, Labels, Windows, View Controllers, all these are objects. When we drag two Buttons into Interface Builder and run the app, we see two Button objects in action. You can work with objects using two ways. They can have attributes (also called
properties) that can be changed. We can do this in Interface Builder or later in our program when the app is running. Have a look at the properties of the
Label object in Interface Builder to get an idea what properties you can manipulate:
The other thing we can do with objects is to talk to them by sending them messages. For example, in a later part of the tutorial, we will go to our
UINavigationController object and ask it to show some other view.
So, objects have attributes which we can manipulate and we can talk to them by sending messages. Now, you might wonder, where does the definition come from what attributes we can manipulate and what messages we can send? Here is the trick: Every object has some kind of a
blueprint which defines these things. This is called a
class. A class like
UIButton defines attributes and messages. When you create a button, your button object is created using this
UIButton blueprint. Now, many classes like
UIViewController were written by the Apple engineers and we have just to use them like we already did. But sometimes we have to create new classes ourselves. And here is another neat trick: We can not only create completely new classes, we also can take an existing class and add our own parts on top (this is called
to create a subclass).
This is what we will do right now. We have this
UIViewController object in our app which is responsible to handle all the action in our
View. The class
UIViewController has lots of attributes we can change and messages we can send. But
UIViewController objects also get messages sent to it and we can add extra behaviour by creating our own variant of the class and reacting on such messages.
Objective C has a very dynamic object model, you can for example call methods reflectively or add methods dynamically at runtime. Because of this, I’ll use the term “to send a message” instead of “to call a method”. We will see all the details, like the syntax for defining classes and sending messages, in later parts of the tutorial. In the meantime, check out the The Objective-C Programming Language.
Now, let’s say we want our
ViewController to do something. Currently nothing happens at all, it is just there, managing our View with the Slider, Button and Label in it. We have this
UIViewController object in our
.xib and if you look closely, you can see that it is created from the class
Lets create a
UIViewController subclass so that we can add our own behavior. To do this, right-click the ShoppingList folder and create a new File:
Choose to create a subclass of
UIViewController and uncheck
With XIB for user interface (we want to connect it to our existing XIB instead of creating another XIB file):
Name it anything you want, for example
We now have two new files in our project:
DemoViewController.m. We will have a look at all the code in these files in great detail in a later part of the tutorial.
For now, let’s just writing some text out to the console when the view is loaded so we can see something happening. Scroll down in the
DemoViewController.m file looking for
viewDidLoad is a message which is sent to objects of our class whenever the view just came to life:
NSLog(@"DemoViewController says hello"); statement to log a message to the console:
Now, one last step is necessary for this to work. We need to change our
XIB so that it doesn’t create a regular
UIViewController object but a
Now run the app and check the console for the message: