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?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With