[iOS]-ViewController lifecycle

We use the display order of the view and the printing situation to represent the function call order, that is, the life cycle of the View Controller

Function introduction:

▪ viewDidLoad: called after the view is loaded. If it is a view loader created in the code, it will be called after the loadView method. If it is output from the nib view page, it will be called after the view is set. It will only be called once, and then enter the ViewController without calling the function.
▪ viewLoad: every time you access the controller's view (such as self.view) and the view is nil, the loadview method will be called. View for creating controller
▪ viewWillAppear: the view will appear
▪ Viewwilllayouubviews: the view of the controller will layout the child controls (in this method, the deployment needs to change the code to refresh the view. The function is similar to the layoutSubViews method of view. Note that this method generally obtains the frame, width and height of the view that needs to be reset, so the frame of view is generally written in this method)
▪ viewDidLayoutSubviews: the view layout child control of the controller is complete
▪ viewDidAppear: the view is already displayed
▪ viewWillDisappear: the view is about to disappear
▪ viewDidDisappear: the view has disappeared. It is called when the Controller is switched. After the second view appears, the first view disappears (but the original view will not disappear when the modal view not displayed in full screen is displayed as a new view)

Code test:

We test by adding multiple uiviews on a view and a Button to jump to the view interface

▪ The code in viewDidLoad of ViewController is as follows:

	//We add a Button in viewDidLoad
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [testButton setTitle:@"Click jump" forState:UIControlStateNormal];
    [testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    testButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 4, [UIScreen mainScreen].bounds.size.width / 5, 50);
    
    [testButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:testButton];

Next are some functions to test the view cycle:
▪ Viewwillappear (we set it to gray):

- (void) viewWillAppear:(BOOL)animated {
    _grayView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    [self.view addSubview:_grayView];
    _grayView.backgroundColor = [UIColor grayColor];
    NSLog(@"grayViewWillAppear");
}

▪ Viewdidappear (we set it to blue):

- (void) viewDidAppear:(BOOL)animated {
    _blueView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    [self.view addSubview:_blueView];
    _blueView.backgroundColor = [UIColor blueColor];
    NSLog(@"blueViewDidAppear");   
}

▪ Viewwilldisappear (we set it to red):

- (void) viewWillDisappear:(BOOL)animated {
    _redView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    _redView.backgroundColor = [UIColor redColor];
    NSLog(@"redViewWillDisappear");
}

▪ Viewwilllayoutsubviews (we set it to black):

- (void) viewWillLayoutSubviews {
    _blackView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    [self.view addSubview:_blackView];
    _blackView.backgroundColor = [UIColor blackColor];
    NSLog(@"blackViewWillLayoutSubviews");
}

▪ Viewdidlayoutsubviews (we set it to yellow):

- (void) viewDidLayoutSubviews {
    _yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    [self.view addSubview:_yellowView];
    _yellowView.backgroundColor = [UIColor yellowColor];
    NSLog(@"yellowViewDidLayoutSubviews");
}

▪ Button event function:

- (void) pressButton {
    NSLog(@"-------------pressButton-------------");
    SecondViewController *second = [[SecondViewController alloc] init];
    second.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:second animated:NO completion:nil];
}

Result when the code is first executed (no interface jump):

The hierarchy diagram is as follows:
After jumping to the second interface:
We also set a button in the second interface to return to the original view
In addition, four functions are set, and corresponding words are output in the function to display the function execution of view 2 and view 1 switching:
▪ The code in viewDidLoad of SecondViewController is as follows:
(set a return button)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [testButton setTitle:@"Click back" forState:UIControlStateNormal];
    
    [testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    testButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2, [UIScreen mainScreen].bounds.size.width / 5, 50);
    
    [testButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:testButton];
}

▪ The four functions are as follows:

- (void) viewDidDisappear:(BOOL)animated {
    NSLog(@"viewSecondDidDisappear");
}

- (void) viewWillDisappear:(BOOL)animated {
    NSLog(@"viewSecondWillDisappear");
}

- (void) viewDidAppear:(BOOL)animated {
    NSLog(@"viewSecondDidAppear");
}

- (void) viewWillAppear:(BOOL)animated {
    NSLog(@"viewSecondWillAppear");
}

▪ Button click event function:

- (void) pressButton {
    NSLog(@"---------------pressBack---------------");
    [self dismissViewControllerAnimated:NO completion:nil];
}

Operation results after clicking:

The hierarchy diagram is as follows:

Note that view 1 does not completely disappear until view 2 is loaded and fully displayed

Operation results after clicking the return button:

The hierarchy diagram is as follows:

Tags: iOS

Posted on Tue, 14 Sep 2021 16:10:45 -0400 by Dark_Storm