需要帮助从 UINavigationController 中解雇模态呈现 ViewController 和弹出 ViewController

Need assistance dismissing modally presented ViewController and popping ViewController from UINavigationController

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];
}

V3V2 模态呈现并且没有代码。

在 运行 应用程序之后,我按下主页按钮并再次打开该应用程序,此触发通知并由 V2 收到。

V2 应该做什么:

  1. 关闭 V3。如果 V3 没有 ViewController 子类,那么它会被忽略,否则不会。
  2. V2 本身必须从 UINavigationController 中弹出,但如果 V3 未被关闭但它不会弹出,但会给出日志 goBackToV1.

如果 V3 我这样做 NSLog(@"%@", [self presentingViewController]); 我得到 <UINavigationController: 0x13582d800>

我的问题:

  1. 为什么 V3 在没有 ViewController 子类分配给它时被解雇。
  2. 为什么 V3 在 ViewController 子类分配给它时不被解雇。
  3. 为什么 V2 上的 performSegueWithIdentifier 没有将其弹出到 V1 尽管代码已执行但其简单被忽略。

首先检查V2中是否有presentedViewController,如果有,则关闭它并在完成块中执行segue,否则直接执行segue,

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