views:

1318

answers:

1

I'm just starting iPhone development (coming fron a .Net world) and have been going through many "Hello World" applications to get the hang of this new development platform. One area I have been confused with is the instantiation of a view controller. On an Apple "Hello World" tutorial, they start by creating a Window Based App, which by default has no view controller. They then create a UIViewController and manually instantiate that controller in the application delegate, followed by simply adding the view controller to the window (http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhone101/Articles/03_AddingViewController.html#//apple_ref/doc/uid/TP40007514-CH5-SW5).

In contrast, if I were to create a new "View based application" project and look at the delegate implementation, all that was done to link that view controller to the main window was the following line: [window addSubview:viewController.view];

Why does the tutorial indicate I must manually instantiate the view controller via alloc/init, when the pre-built "view based application" template simply adds the view controllers view to the window? What's the difference between the two?

A: 

From doing some more digging, I found out the answer to my own question. When you create a View Based Application, you'll notice that if you double-click the MainWindow.xib, one of the objects in the Document Window is a View Controller which is already connected to the [ProjectName]ViewController class (a class which was automatically created when you selected the View Based Application).

Hence, by dragging in a View Controller from the Library to the Document Window in MainWindow.xib, you are in essence INSTANTIATING that object (although it's archived in the nib file). Therefore, there is no need to instantiate it manually in the application delegate. Simply add that view controllers' view to the window and you're done.

tbehunin