A Simple Experiment in View Controller Lifecycle

Lecture 5 from “Developing iOS7 Apps for iPhone and iPad (Fall 2013) by Stanford University” explained View Controller life cycles. Here is a simple experiment to see it in action.

Notice the viewDidLoad method runs only once. Whereas viewWillAppear and viewDidAppear runs every time the view comes alive. And as the view goes away, viewWillDisappear and viewDidDisappear runs.

Even though it’s almost a child-like experiment, it’s the experience that helps reinforce learning. To experiment on your own, create an Xcode project and choose Tabbed Application from the templates. In the FirstViewController.m file, add the following code. We simply print out a log message as a view appears and disappears from screen.

- (void)viewDidLoad
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    NSLog(@"Step 1: view did load");

- (void)viewWillAppear:(BOOL)animated
    [super viewWillAppear:animated];
    NSLog(@"Step 2: view will appear");

- (void)viewDidAppear:(BOOL)animated
    [super viewDidAppear:animated];
    NSLog(@"Step 3: view did appear");

- (void)viewWillDisappear:(BOOL)animated
    [super viewWillDisappear:animated];
    NSLog(@"Step 4: view will disappear");

- (void)viewDidDisappear:(BOOL)animated
    [super viewDidDisappear:animated];
    NSLog(@"Step 5: view did disappear");

Feel free to add more sophisticated views and watch them in action 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s