S.J
S.J

Reputation: 3071

Need assistance dismissing modally presented ViewController and popping ViewController from UINavigationController

enter image description here

AppDelegate

- (void)applicationWillEnterForeground:(UIApplication *)application {
    NSLog(@"applicationWillEnterForeground");
    [[NSNotificationCenter defaultCenter]postNotificationName:@"applicationWillEnterForeground" object:nil];
}

V1

-(IBAction)uw:(UIStoryboardSegue*)segue{
    NSLog(@"Back on V1");
}

V2

-(void)awakeFromNib {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(goBackToV1) name:@"applicationWillEnterForeground" object:nil];
}

-(void)goBackToV1 {
    NSLog(@"goBackToV1");
    [self performSegueWithIdentifier:@"uwid" sender:nil];
}

V3 present modally from V2 and has no code.

After running the app I hit the home button and open the app again, this trigger notification and received by V2.

What V2 is supposed to do:

  1. Dismiss V3. If V3 has no ViewController subclass then it is dismissed otherwise its not.
  2. V2 itself must be popped from UINavigationController, but it do not pop if V3 is not dismissed but gives the log goBackToV1.

If on V3 I do this NSLog(@"%@", [self presentingViewController]); I get <UINavigationController: 0x13582d800>

My question:

  1. Why V3 get dismissed when no ViewController subclass is assigned to it.
  2. Why V3 do not get dismissed when ViewController subclass is assigned to it.
  3. Why performSegueWithIdentifier on V2 didn't pop it to V1 although the code get executed but its simple got ignored.

Upvotes: 0

Views: 56

Answers (1)

ogres
ogres

Reputation: 3690

First check if you have presentedViewController in V2, if you do, then dismiss it and in completion block perform the segue, otherwise perform the segue directly,

-(void)goBackToV1 {
    NSLog(@"goBackToV1");
    if(self.presentedViewController) {
        [self dismissViewControllerAnimated:YES completion:^{
            [self performSegueWithIdentifier:@"uwid" sender:nil];
        }];     
    } else {
        [self performSegueWithIdentifier:@"uwid" sender:nil];
    }
}

Upvotes: 1

Related Questions