Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: What is the difference between -init and -viewLoad of a ViewController?

Tags:

I don't know exactly what's the right place to set things like the tintColor of the NavigationBar or the title of my ViewController. It works in the -init method and the -viewLoad method too. What is the "best-practice" or "right way" to do this? Has one of those any advantages?

like image 428
d4n13l Avatar asked Dec 29 '11 09:12

d4n13l


People also ask

What is the difference between a view and a Viewcontroller?

A view controller is not drawable to the screen directly, it manages a group of view objects. View controllers usually have a single view with many subviews. The view controller manages the state of these views. A view controller is smart, and it knows how to interact with your application.

What is a Viewcontroller iOS?

A view controller acts as an intermediary between the views it manages and the data of your app. The methods and properties of the UIViewController class let you manage the visual presentation of your app. When you subclass UIViewController , you add any variables you need to manage your data in your subclass.

What is Xcode Viewcontroller?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.

What is the difference between Table View and Table View Controller?

A table view is an instance of the UITableView class, which is a subclass of UIScrollView. Table view cells, which are the repeatable rows, or views, shown in the table view. A table view cell is an instance of a UITableViewCell class, and that class is often subclassed to create custom table view cells.


2 Answers

The init methods (yes there is more then one) are where the UIViewController is initialized. Thus this is the place where you do stuff for the UIViewController and not its views.

If you use a nib to load you view then the best place to set any properties is the viewDidLoad method. This method gets called after the nib is loaded. If you set up the view programatically use the loadView method then this is the place to set UIControl properties.

Since the system can unload views to save memory, it will leave the UIViewController alone. Any properties set in the init methode will not be applied again, since the UIViewController is already initialized.

like image 191
rckoenes Avatar answered Oct 31 '22 19:10

rckoenes


the init method is used to initialize the viewController while viewDidLoad method is used to load your nib(i.e. your view). so when you want to do something with your viewController then use init method and when you want to do something with your view then use viewDidLoad.

like image 36
Rohan Avatar answered Oct 31 '22 19:10

Rohan