【问题标题】:Need assistance dismissing modally presented ViewController and popping ViewController from UINavigationController需要协助解除模态呈现的 ViewController 并从 UINavigationController 弹出 ViewController
【发布时间】:2016-03-15 09:59:29
【问题描述】:

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 从V2 模态显示并且没有代码。

运行应用程序后,我点击主页按钮并再次打开应用程序,此触发通知并由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 尽管代码已执行但其简单却被忽略了。

【问题讨论】:

    标签: ios objective-c uistoryboard uistoryboardsegue nsnotificationcenter


    【解决方案1】:

    首先检查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];
        }
    }
    

    【讨论】:

    • 感谢您解决了我的问题,请问您也可以根据我问的问题解释一下吗?
    • 可能是因为您没有关闭 V3,因此 V2 仍然需要在视图层次结构中,因为它是 V3 的呈现视图控制器
    • 但是为什么在情节提要中没有为 V3 分配自定义视图控制器类时会自动关闭它?
    猜你喜欢
    • 2018-03-07
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2020-09-08
    • 2019-09-18
    • 1970-01-01
    相关资源
    最近更新 更多