Search code examples
iosobjective-cxcodeviewwillappearpoptoviewcontroller

viewWillAppear not called after popToViewController


I have a little problem. I am working on a simple application with Views Controllers in a Navigation Controller like this: A->B->C (-> are modal segues) View A is the Root View Controller and I need to come back to A from C. If I call the method popToViewController from B, A run the viewWillAppear; if I call the popToViewController from C (to A), viewWillAppear on A is not called. How can I solve this? (Working on Xcode7 and iOS 9)

ViewController A

#import "ViewControllerA.h"
#import "ViewControllerB.h"

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setupSceneA];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"goToB"]) {
       ViewControllerB *b = [segue destinationViewController];
    }
}

ViewController B

#import "ViewControllerB.h"
#import "ViewControllerC.h"

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setupSceneB];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"goToC"]) {
       ViewControllerC *c = [segue destinationViewController];
    }
}

- (IBAction)backToAButton:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

ViewController C

#import "ViewControllerC.h";

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self setupSceneC];
}


- (IBAction)backToBButton:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (IBAction)backToAButton:(id)sender {
    [[self parentViewController] dismissViewControllerAnimated:NO completion:nil];
}

Solution

  • This should be your design:

    Controller A -> modal segue to Nav Controller(Controller B is root view controller) -> push segue to controller C.

    So if you want to come from C to B, you do a pop.

    When you want to come from B to A or from C to A, you do a dismiss.