Why my viewWillAppear method is not calling everytime i go back to my parent view

Viewed 105

This is my viewController.m file

- (IBAction)defaultAction:(id)sender {
    SimpleViewController *simpleView = [[SimpleViewController alloc]init];
    [self presentViewController:simpleView animated:YES completion:nil];
}

- (IBAction)flipAction:(id)sender {
    SimpleViewController *simpleView = [[SimpleViewController alloc]init];
    [simpleView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentViewController:simpleView animated:YES completion:nil];
}

- (IBAction)dissolveAction:(id)sender {
    SimpleViewController *simpleView = [[SimpleViewController alloc]init];
    [simpleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:simpleView animated:YES completion:nil];
}



- (IBAction)pageCurlAction:(id)sender {
    self.callFromPageCurl = true;
    SimpleViewController *simpleView = [[SimpleViewController alloc]init];
    simpleView.delegate = self;
    [simpleView setModalPresentationStyle:UIModalPresentationFullScreen];
    [simpleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
    [self presentViewController:simpleView animated:YES completion:nil];
}

- (IBAction)showWithDelegate:(id)sender {
    self.callFromShowWithDelegate = true;
    SimpleViewController *simpleView = [[SimpleViewController alloc]init];
    simpleView.delegate = self;
    [self presentViewController:simpleView animated:YES completion:nil];
}


- (void)didReceiveMessage:(NSString *)message{
    self.myMessage.text = message;
}

- (void)viewWillAppear:(BOOL)animated{
    self.callFromShowWithDelegate = false;
    self.callFromPageCurl = false;
    NSLog(@"Called first");
}

and this is my simpleViewController.m file

- (IBAction)dismissMeAction:(id)sender {
    if(self.delegate.callFromShowWithDelegate)
        [self.delegate didReceiveMessage:@"Hello World"];
    else
        [self.delegate didReceiveMessage:@"My Message"];
    if(self.delegate.callFromPageCurl == true)
        [self dismissViewControllerAnimated:NO completion:nil];
    else
        [self dismissViewControllerAnimated:YES completion:nil];
}

When i use pageCurl to go to the next view and i came back to the main view it is calling viewWillAppear method again But if i go to the next view using any other button and came back to the main view it is not calling viewWillAppear method.. But why? SHouldn't it have called viewWillAppear method every time i came back to main view.

1 Answers

If you have used modal presentation for view controller prior to iOS 13, the default behavior is presenting the modal view controller in full screen, and when the modal view controller is dismissed, the viewDidAppear function will be called on the presenting view controller (ie. the view controller that is responsible for presenting the modal view controller).

However in iOS 13, the default behavior of presenting the modal view controller is replaced with a card-like animation (the official term for it is page sheet)


In iOS 13+, You need to set the correct presentationStyle. If you want that your presentedController will be fullScreen and call previous viewWillAppear, then you can use "UIModalPresentationFullScreen"

- (IBAction)defaultAction:(id)sender {
    SimpleViewController *simpleView = [[SimpleViewController alloc]init];
    [simpleView setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:simpleView animated:YES completion:nil];
}

You can change this in your nib/storyboard as well:

enter image description here

Related