Nicole Han

iOS View Controller

Word count: 706Reading time: 4 min
2019/03/30 Share

iOS View Controller

Work with view controller

Understand the View Controller Lifecycle

  • An object of the UIViewController class (and its subclasses) comes with a set of methods that manage its view hierarchy. iOS automatically calls these methods at appropriate times when a view controller transitions between states.
  • When you create a view controller subclass, it inherits the methods defined in UIViewController and lets you add your own custom behavior for each method. It’s important to understand when the system calls these methods, so you can set up or tear down the views you’re displaying at the appropriate step in the process.

life cycle - view controller

iOS calls the UIViewController methods as follows:

  • viewDidLoad()—Called when the view controller’s content view (the top of its view hierarchy) is created and loaded from a storyboard. The view controller’s outlets are guaranteed to have valid values by the time this method is called. Use this method to perform any additional setup required by your view controller.
  • Typically, iOS calls viewDidLoad() only once, when its content view is first created; however, the content view is not necessarily created when the controller is first instantiated. Instead, it is lazily created the first time the system or any code accesses the controller’s view property.

  • viewWillAppear()—Called just before the view controller’s content view is added to the app’s view hierarchy. Use this method to trigger any operations that need to occur before the content view is presented onscreen. Despite the name, just because the system calls this method, it does not guarantee that the content view will become visible. The view may be obscured by other views or hidden. This method simply indicates that the content view is about to be added to the app’s view hierarchy.

  • viewDidAppear()—Called just after the view controller’s content view has been added to the app’s view hierarchy. Use this method to trigger any operations that need to occur as soon as the view is presented onscreen, such as fetching data or showing an animation. Despite the name, just because the system calls this method, it does not guarantee that the content view is visible. The view may be obscured by other views or hidden. This method simply indicates that the content view has been added to the app’s view hierarchy.

  • viewWillDisappear()—Called just before the view controller’s content view is removed from the app’s view hierarchy. Use this method to perform cleanup tasks like committing changes or resigning the first responder status. Despite the name, the system does not call this method just because the content view will be hidden or obscured. This method is only called when the content view is about to be removed from the app’s view hierarchy.

  • viewDidDisappear()—Called just after the view controller’s content view has been removed from the app’s view hierarchy. Use this method to perform additional teardown activities. Despite the name, the system does not call this method just because the content view has become hidden or obscured. This method is only called when the content view has been removed from the app’s view hierarchy.


MVC Pattern

This style of app design where view controllers serve as the communication pipeline between your views and your data model is known as MVC (Model-View-Controller). In this pattern, models keep track of your app’s data, views display your user interface and make up the content of an app, and controllers manage your views. By responding to user actions and populating views with content from the data model, controllers serve as a gateway for communication between the model and views.
MVC is central to a good design for any iOS app.

Development steps

  1. provide a launch screen as an Xcode storyboard or as a set of static images for the devices your app supports
  2. generate image sets for different sizes - use ImageMagick from Github or online tools
  3. 5.

In conclusion, launch screens can be a way to bring a little delight to your users or an opportunity to reinforce your brand identity. Whatever reason you design your launch screen for, make sure that it doesn’t cause the loading time to go on any longer than it should.

CATALOG
  1. 1. iOS View Controller
    1. 1.1. Work with view controller
    2. 1.2. Understand the View Controller Lifecycle
      1. 1.2.1. iOS calls the UIViewController methods as follows:
  2. 2. MVC Pattern