In this post we will have a close look at the difference between UIView and UIViewController.
UIView and UIViewController both are classes and are of equally important but both the classes have their own specialty. First we will have a discussion of UIView and then UIViewController to understand the concepts of both the classes.
UIView: UIView is a class which holds the other views, in iPhone each and every control is refereed as a view. You can add a view inside another view and to hold all these views you need a container and your UIView class acts as container where you can add another views like the buttons,labels, switch, slider etc because of this the UIView can have the parent and child relationship between them know as the subviews and the parent view doing the embedding is called as the superview. Every UIView is related with UIViewController which manages the UIView.
Some Important instance method of UIView class
addSubview: It takes a parameter of the UIView instance (label,buttons, etc) which is to be added.
drawRect: This method is used when you want to draw something in the view like circle, rectange etc.
didAddSubView: Informs whether a subview was added.
removeFromSuperView: It removes a view from the parent view and from the responder chain.
UIViewController: UIViewController class is used to control the UIView instance (object) and the view which are present in the UIView (buttons,labels,table). The ViewController manages the modal view, device rotation, memory management etc, Subclasses such as UINavigationController and UITabbarController provide additional behavior for managing complex hierarchies of view controllers and views.The UIViewController has a view property with the help of which you can refer to the view which it controls, now this view can have another view like the button,label,table etc.
Some important instance method of the UIViewController class:
loadView: In this method you add the controls to the self view but before that you make a call to the super class loadview method so that the view that you are going to add in the self view are being added properly. This method is called when the view property of the viewController is accessed and you must use the loadView method only when you are adding views programmatically (without using Interface builder).
viewDidLoad: This method always gets called when we want do some manipulations with the views after they are loaded in the memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method.
viewDidAppear: Notifies the view controller that its view was added to a window.
viewDidDisappear: Notifies the view controller that its view was dismissed, covered, or otherwise hidden from view.
viewWillAppear: Notifies the view controller that its view is about to be become visible.
viewDidUnload: It is called when the when the controller’s view is released from the memory.