A little bit about ViewControllers

Member
Posts: 75
Joined: 2009.01
Post: #1
I've recently figured out how to switch between views of UIView subclass.
When I usually make a project I initialize the window with a main view and go about coding.
But now I need to use a view controller for my project and I have two problems:
One, I don't know EXACTLY what they really do. I heard they are for switching between multiple views in a larger project, but i've been doing fine with just UIView subclass...
Second, I tried add a view to the view controller and I get nothing.
Code:
- (void)loadView {
     [self.view addSubview:mainView];
}

That's what I thought would work but it's just not simple since I don't know what i'm dealing with exactly.

Anyone use view controllers?
Could you give me a few pointers?
Quote this message in a reply
⌘-R in Chief
Posts: 1,256
Joined: 2002.05
Post: #2
I was going to brush up on the docs a bit to help explain it to you, but... the docs explain it pretty well to begin with.

https://developer.apple.com/iphone/libra...riPhoneOS/



The shortest explanation I can give is to say that view controllers are just that... controllers. They're not "for switching views", they're a class of object that fits into the MVC design pattern which you can use to fill the gap between your models and views. That can mean many things depending on the situation.

Look at the existing view controller subclasses and you can get idea of what they're for.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2009.03
Post: #3
here is a quote from Erica Sadun's The iPhone Developers Cookbook:

Quote:First and foremost, view controllers are there to make your life easier. They take responsibility for rotating the display when a user reorients his or her iPhone. They resize views to fit within the boundaries when using a navigation bar or a toolbar. They handle all the interface’s fussy bits and hide the complexity involved in directly managing interaction elements. You can design and build iPhone applications without ever using a UIViewControlleror one of its subclasses, but why bother? The class offers so much convenience it’s hardly worth writing an application without them.

cheers! Smile
Quote this message in a reply
Post Reply