Controllers

UIViewController base class for view controllers. Typically you don't need use it directly. Main responsibilities of view controller are:

  • updating the contents of the views, usually in response to changes to the underlying data
  • responding to user interactions with views
  • resizing views and managing the layout of the overall interface
  • coordinating with other objects—including other view controllers—in your app

View controllers are UIResponder objects and are inserted into the responder chain between the view controller’s root view and that view’s superview, which typically belongs to a different view controller. If none of the view controller’s views handle an event, the view controller has the option of handling the event or passing it along to the superview.

Most usefull controllers are:

  • UINavigationController implements a stack-based navigating
  • UIPageViewController manages navigation between pages of content, where a child view controller manages each page
  • UITableViewController allows to show table (list of items)

lyfe cycle

Below main methods that represent life cycle of controller.

method description
loadViewIfNeeded() Loads the view controller’s view if it has not yet been loaded (iOS 9+).
viewDidLoad() Called after the controller's view is loaded into memory.
viewWillAppear(_ animated: Bool) Notifies the view controller that its view is about to be added to a view hierarchy.
viewWillLayoutSubviews() Called to notify the view controller that its view is about to layout its subviews. Your can override this method to make changes before the view lays out its subviews. Used when you are not using constraints or Auto Layout.
viewDidLayoutSubviews() Called to notify the view controller that its view has just laid out its subviews, in other words, subviews have been setup.
viewDidAppear(_ animated: Bool) Called when the view has been fully transitioned onto the screen.
viewWillDisappear(_ animated: Bool) Notifies the view controller that its view is about to be removed from a view hierarchy.
viewDidDisappear(_ animated: Bool) Notifies the view controller that its view was removed from a view hierarchy.