position: fixed; top: auto !important; margin-left: 112px;

What is UIViewController & UIView?

The basic difference between UIView & UIViewController is UIView is a class that represents the screen of the device which is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.

The word UIViewController itself tells its meaning that is, a controller which controls the View and its logic. This is the place where you can add as many views you want.

What is UIView?

UIView is a place which tells your iPHone's full screen to give me some part of the screen so that I can define it in my own way

Example :- A part of screen with height 60.

Viewcontroller's View Life Cycle

Basically these are the methods inside view controller which is being called when app starts.

1) viewDidLoad- It is automatically called when the view controller is loaded completely in the memory. Initialisation of Objects is done in this method.

So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without those forms.

Example:

viewDidLoad is used to initialize setup of the interface or when you want to create database access or when you want to prepopulate the data etc.


2) viewWillAppear - This is called every time the view is about to appear, whether or not the view is already in memory. Put your dynamic code here, such as model logic.

Example:

1) Update navigation bar style
2) Update status bar style
3) Update screen data
4) Orientation handling


3) viewDidAppear - Put expensive operations here that you only want to do if you're sure the view is onscreen, such as network calls.It is called when the view controller's view was added to the view hierarchy.

Example:

1) Start UI animation
2) Display loader view


4) viewWillDisappear - It is called when the view controller's view is about to be removed from the view hierarchy.

Example:

1) Hide keyboard
2) Revert changes that were made in viewWillAppear method.

5) viewDidDisappear - It is called when the view controller's view was removed from the view hierarchy.

Example:

1) Remove cache data.

Practical Example

If you do not beleive :), I can illustrate this with a practical example. What we do in this sample app is we simulate all the methods inside Viewcontroller which is being called when an app launches.

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"View Did Load method is called");
    
    self.view.backgroundColor = [UIColor whiteColor];
  
    UIButton *buttonPushMe = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 48, CGRectGetMidY(self.view.frame) - 30, 120, 40)];
    [self.view addSubview:buttonPushMe];
    [buttonPushMe setTitle:@"Push Me" forState:UIControlStateNormal];
    [buttonPushMe setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonPushMe addTarget:self action:@selector(pushMeClicked) forControlEvents:UIControlEventTouchUpInside];
    buttonPushMe.backgroundColor = [UIColor orangeColor];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"View Did Appear methods is called");
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"View Will Appear methods is called");
}

- (void)pushMeClicked
{
    SecondViewController *secondViewController = [[SecondViewController alloc]init];
    [self presentViewController:secondViewController animated:YES completion:nil];
}
@end

Output:


On initial startup of app, viewDidLoad() method is called first when the entire view is loaded into memory. Then, viewWillAppear() method and finally viewDidAppear() method is called.

Now, when I clicked Push Me button next view controller is opened. It looks like this,

Then when we leave this view controller on the action of back button, only viewWillAppear() & viewDidAppear() methods are called. This shows, viewDidLoad() method is only called once on inital startup of app.

I hope you understood everything about the View controller life cycle and once again Enjoy learning :). See you next episode.


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.