1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Advanced iOS Features: Permanent Features, Segue, Alert Message, Timers, and Gestures

ViewController Lifecycle


Advanced iOS Features
Storing Data
PREVIEW11m 37s
Second Screen
14m 39s

The course is part of this learning path

Start course
1h 57m

This course explores some advanced iOS features. We'll look at how to store data permanently. You'll learn how to work with more than just one screen, how to use Gesture Recognizers, Timers, Alert Messages and so much more. You're going to have essential skills for iOS development after you complete this course.

Intended Audience

This course is intended for beginners who want to learn how to build apps using Swift.


To the most out of this course, you should have some basic understanding of programming and computer science in general.



Hi, within this lecture, we're going to learn about ViewController Lifecycle. So, what does it even mean?

So, a ViewControllers has actually a lifecycle; they get created, they get disappeared, they will reappear one more time. And there are certain functions like viewDidLoad over here, which actually defines what will happen when that event is going to be executed. For example, what do we do in the viewDidLoad? We want to write what will happen when user first opens their app. But in fact, there are a lot more functions like this, and we need to learn about them so we can leverage them in a better way. So, let me open my simulator because I'm going to run and show you in the simulator in real-time. We're going to call these other functions, and we're going to see when and where they're going to be executed. So, when I go to 2nd ViewController for instance, what happens in the background? So, when I go back what happens in the background, we'll try and see that.

So, we have two different views over here. But I'm going to show these functions inside of our ViewController.swift so that we can keep track what's being called. So, let me show you the other functions that we have been talking about so that you will understand it better in an example. So, if you come over here under your viewDidLoad, we are not inside of viewDidLoad; we're inside of class. So, if you write viewdid, you can see viewDidDisappear like viewDidLoad. So, we're going to call this viewDidDisappear. So, as you can see, this notifies the ViewController that view is actually disappeared. So, we have a lot of functions like this. And in order to keep track what's being called, I'm just going to print the names. 

So, ("viewDidDisappear function called"). So, we can actually do what we want in this viewDidDisappear, like we can set our labels, we can just delete our data; we can do whatever we want. But for right now, I'm just going to call all of this. For example, viewWillDisappear. I'm going to print out so we can see then and why do they get called? So, ("viewWillDisappear function called"), and we have some more. Let me write view, we have viewWillAppear. And the names are actually self-exploratory. So, this will be called when the view is about to appear. And ("viewWillAppear function called") and viewDidLoad, viewDidDisappear, viewWillDisappear and we should have one more, and by the way, you may think that why did you wait so much in order to tell us about this?

I waited enough so that we will have two views to test this. So, you will understand it much better. So, lastly, I'm going to call viewDidAppear. So, this is view is about to appear, and the below one, view has appeared already. And we're going to see that in action as well. Don't worry. So, ("viewDidAppear function called"). So, here we go. We have a lot of functions defining our ViewController lifecycle over here. So, let me call this, and let me open the logs. And once I opened the logs, I didn't even do anything yet,

but as you can see, viewDidLoad, viewWillAppear and viewDidAppear called, and you have to pay attention to the order. First, viewDidLoad has been called; second, viewWillAppear; and then viewDidAppear. So, viewDidLoad actually gets called before we see the view. Before the user sees the view, viewDidLoad gets called. So, for example, let me go to 2nd view. And as you can see, viewWillDisappear and viewDidDisappear gets called. So, right now, my 1st ViewController did disappear. And now if I go back, what do you think will happen? Will viewDidLoad be called again? As you can see, viewDidLoad is not called again. So, viewWillAppear and viewDidAppear got called again. So, viewDidLoad only gets called once the app is launched. After that if you go to this views without killing this ViewController, it never gets called. So, if I want something to be happening every time user goes back and forth, I cannot use viewDidLoad.

So, if I want something to be happening when I go back, I have to use viewWillAppear or viewDidAppear. I cannot use viewDidAppear; it won't make sense. I can use viewWillAppear. For example, let me write atil and go to 'Next'. So, I'm going into the second screen. So, viewWillDisappear and viewDidAppear called. And I go back, and I still see the atil. And this is default behavior; we expect to see that. But what if what I want to get rid of this name? What if what I want this name to be empty when I go back? Can I do that under viewDidLoad? So, let me try this nameText.text is empty string. So, let me run this one more time. And, of course, we won't do that because viewDidLoad is called right now, but it won't get called when I go here and go back. So, since viewDidLoad isn't called, nameText.text is not an empty string anymore. So, it doesn't work. So, we have to know the details of these functions in order to leverage them.

viewDidLoad only gets called once the application is launched. Now if I do, if I want to do something like that, I have to do this under viewWillAppear so that it will be in place before the user sees it. So, I'm not doing that under viewDidAppear. I'm doing it under viewWillAppear so it will be in effect before user sees it, and it will get called every time this view appears. So, let me go to atil and it 'Next' and go back. And this time, as you can see, my nameText.text is actually empty. So, this is how you leverage these functions. We're going to see some other examples of that when we build apps in the following sections. So, I'm going to stop here, and we're going to learn about alert messages within the next lecture.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics