views:

42

answers:

1

I am a newbie in iphone dev, and I am confused by UIViewController.

most program start with a view controller, but I don't know why we need it. ViewController has a view, but you cannot use the drawRect medthod. So why not just use View instead of ViewController in places like UIWindow and so on?

I learned that to initialize a View, you need to use initWithCoder, because view is in the nib file. But why view is in the nib file? I subclass UIView and do some drawing in drawRect, what is its connection with nib file ?

+1  A: 

You can open the nib file in Interface Builder so you can build the user interface graphically instead of programatically. So you don't need to drawRect, you can just build your interface through interface builder.

UIViewControllers contain UIViews. Apple believes in Model-View-Controller software architecture, in which the data is separated from the view, but the data and view are joined by a controller class, which handles application logic.

Model < -- > Controller < -- > View

CustomDataClass < -- > UIViewController < -- > UIView

jmont
the NSObject near DataClass is misleading since even UIViewController and UIView are NSObjects
rano
You're right! I edited my answer, I hope it no longer misleads.
jmont
Thank you for the information! Although I think UIViewController shouldn't contain a view by itself.