views:

2467

answers:

1

Hi everyone. I'm currently learning Objective-C, coming from a Java background where I have no trouble making GUI applications. As such, the way I'm thinking about some of these problems may be a bit off. Here's the question (please excuse the ramble) -

What is the purpose of UIViewController?

Let me give a specific example. Say you're creating a one-view app that uses a bunch of GUI widgets. You can lay them out by hand in code or you can use InterfaceBuilder. In other languages such as Java and HTML, I always code things by hand and don't use GUI tools as I've found that they haven't allowed me to do the customization I've required. Having said that, I've heard that InterfaceBuilder is the Sweetness (do experts do it by hand?). So let's say I lay out a bunch of widgets in a XIB file and then want to load this UIView in code.

It seems to me (am I wrong?) that one needs to use a UIViewController in order to have proper de-nibbing of a UIView, but for my apps purposes, I have no use for the controller itself (let's leave MVC aside here for a minute). So do I have to create the UIViewController for this simple one-view app?

Let's extend that to a two-UIView app case. In this case, I have an app with two views, one of which is totally custom with no widgets (drawRect here I come) and the other view is widget-tastic (settings, etc.). I want to be able to swap the views, one of which I've created in IB and the other which is pure code. I'm creating the custom-painted UIView as follows:

eyesView = [[EyesView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];")

In the case of the widget view, I'm using:

settingsView = (SettingsView *)[[[UIViewController alloc] initWithNibName:@"SettingsView" bundle:nil] view];

But I can't seem to figure out a way to awaken the XIB without using this dummy UIViewController which I have no use for. This brings me back to the original question - what is the purpose of the UIViewController? Is it only a nicety that Apple has included to let developers get up and running with navigation panes and their like easily (I don't imagine so)?

Is there a better way to be doing all this?

Thanks!

+6  A: 

UIViewController is the de-facto Controller class and is where your business logic should reside (either here or in other Controller-style classes). If you aren't familiar with the Model-View-Controller design pattern, you should read about it.

Anyway, yes, if you're deserializing stuff from a nib, you need an object that acts as the File's Owner, and except in the case of MainMenu.xib this object should almost certainly be a subclass of UIViewController. I can't think of any reason why you would want to leave out the UIViewController.

Technically, it is possible to load a nib by hand using the lower-level nib-loading architecture, but you still need some object to act as File's Owner, and there's no good reason to do this unless you really know exactly what you're doing. Especially since UIViewController gives you a lot of freebies, such as it manages the memory for all the root objects in the nib for you, and it can unload its view under memory pressure if it's not being used (and then re-load it when it gets accessed again).

Kevin Ballard