View Controller Essentials :
- An instance of a subclass of UIViewController
- It manages a view hierarchy
- It’s responsible for creating view objects, handling events
View in View Controller :
- It’s the root of view controller’s view hierarchy
- Two ways of creating its view hierarchy :
- Programmatically, override the UIViewController method loadView
- Interface Builder, loading a NIB file
Create view programmatically :
In your own view controller class(which can be created by inheriting UIViewController), override the loadview:
However, the code above is for creating view hierarchy in view controller only, it has nothing to do with the actual view appeared on the screen. To connect a view in view controller with screen, you need to use UIwindow’s setRootViewController.
Create view use XIB :
- Steps for creating view for view controller using XIB:
- create a view using interface builder(i.e. create xib file)
- create your view controller for this xib
- make sure your xib’s name is named the same as your view controller (xcode uses this to automatically load nib file i.e. init the view)
- connect your controller to your xib(e.g. File’s owner, IBOutlet, IBAction)
After you get your xib and view controller done, you may now connect these two together.
- Connect to File’s Owner in xib
- use identity inspector to change the view controller class to your own class
- Connect outlet and actions
- right click on File’s Owner, drag-and-drop those connections between your controller and xib
UIViewController Initializer :
- Designated Initializer:
initWithName:bundle:
- Simply call alloc and init
- Sending init to a view controller calls initWithName:bundle: and passes nil for both arguments
- When a view controller is initialized with nil as its NIB name, it searches for a NIB file with the name of the class
UIViewController Lifecycle :
See previous post for more details: View Controller Lifecycle
Reference
- Stanford iOS 7 development
- Mac Developer Library
- Big Nerd Ranch