01 logo

iOS Native Development - ViewController life cycle

Life cycle methods of a ViewController

By ConficlePublished 3 years ago 4 min read
3

Hello, we are back with our next article in a series of native iOS development. In case you would like to refer to our previous articles, you can find them here.

In this article, we will explore different life cycle methods of a ViewController. We believe all small things give you a vast understanding of bigger things. So this article covers those small things, which we are using knowingly or unknowingly or never bother.

Now as we already know that after didFinishLaunchingWithOptions method getting called from Appdelegate.swift file execution flow goes to the initial ViewController file.

Below life cycle methods get called in sequential order.

Let's have a look at each of these and the significance of the same.

Before jumping on this let's understand a few facts related to storyboard file and its inner content

Whenever you create or add any view controller on storyboard below is the shown hierarchy gets created

Indicated view gets generated by default is called as Content View of ViewController

Refer above-mentioned content view in below-mentioned life cycle’s method.

There are different ways to create UI in iOS application development

  • Programmatically
  • Using XIB
  • Using Storyboard

1. loadView: We need to override this method if we want to create a custom view.

As per Apple documentation if you use Interface Builder to create your views and initialize the view controller, you must not override this method. When using Storyboards, this is the method that will load your nib and attach it to the view, but when instantiating view controllers manually, all this method does is create an empty UIView. You can override it to change this behavior and add any kind of view to the view controller's view property.

2. viewDidLoad: This is the first method that gets called WHEN CONTENT VIEW CREATED IN MEMORY. As the content view is loaded from storyboard all IBOutlets will have a guaranteed value in this method so this is the place or method where we usually do stuff like setting up UI (what user should see first ) Or calling API to fetch data.

As the name suggests this method is called when view is loaded in memory. This method gets called only once in complete life cycle.

3. viewWillAppear: After viewDidLoad this method gets called BEFORE CONTENT VIEW IS ADDED TO APP’s VIEW HIERARCHY

This method gets called every-time when view is about to appear to user. As the name suggests this method is called before view is going to appear to user. So this is the place where you can add code that needs to execute every time.

3. viewDidAppear: After viewWillAppear, this method gets called AFTER CONTENT VIEW IS ADDED TO APP’s VIEW HIERARCHY

This method gets called every-time after view appeared to user. Calling of this method guarantees that view is on the screen.

5. viewWillDisappear: This gets called BEFORE CONTENT VIEW IS REMOVED FROM APP’s VIEW HIERARCHY

Here you can do stuff like saving Or committing data e.g. if you have a form and before the user dismissing the form or closes abruptly you can save data here.

As the name suggests this method is called everytime before the view is about to disappear.

6. viewDidDisappear: This gets called AFTER CONTENT VIEW IS REMOVED FROM APP’s VIEW HIERARCHY.

As the name suggests this method is called everytime after the view is has disappeared.

And after this less commonly used methods are

7. viewWillLayoutSubviews: This gets called WHEN THE CONTENT VIEW’S BOUND CHANGE, BUT BEFORE IT LAYS OUT IT SUBVIEWS

8. viewDidLayoutSubviews: Apple has a good theory in this stating that It is called to notify the view controller that its view has just laid out its subviews.

In simple words this method gets called WHEN THE CONTENT VIEW’S BOUND CHANGE, BUT AFTER IT LAYS OUT IT SUBVIEWS.

So whenever the view is updated, rotated, or changed or it’s bounds change. The keyword here is bounds change.

This is a place where you can do anything like adding animation or something like that.

A common example of a bound change in iOS application development is when you rotate the phone from portrait to landscape and in MacOs application development is when the window size changes i.e. if the user maximize or minimize the window

Knowing the basics or significance of this is important to take respective actions in your code especially working with custom implementations of UIKit classes.

That’s it for this article. We will bring more regarding project structure, view controller life cycle in our next articles. Till then please stay tuned.

Thanks for reading this article. If you have any queries related to this topic or iOS, Objective C and Swift. Please write us at [email protected] or direct message us on instagram at conficle(instagram username).

Also keep watching this space for upcoming articles on iOS development, software development and technology concepts.

how to
3

About the Creator

Conficle

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.