在呈现模态视图控制器后推出导航控制器

7

我有一个标签视图控制器,其中有一个按钮,如下所示,当按下它时会出现一个模态窗口:

PostViewController *post = [[PostViewController alloc] init];

// [self.navigationController pushViewController:post animated:YES];

// Presentation
[self presentViewController:post animated:YES completion:nil];

当模态框完成时,我想要关闭它并推出一个新的视图控制器,代码如下:

ProfilesViewController *profile = [[ProfilesViewController alloc] init];
[self.navigationController pushViewController:profile animated:YES];

但是作为一个模态框,我不能在帖子VC中这样做。 我该怎么做?


为什么你需要在出现之前就展示和关闭它? - E-Riddie
2个回答

7
你可以尝试使用completionBlock
当调用presentViewController完成后,将会调用CompletionBlock
PostViewController *post = [[PostViewController alloc] init];
[con presentViewController:post animated:YES completion:^{
    ProfilesViewController *profile = [[ProfilesViewController alloc] init];
    [self.navigationController pushViewController:profile animated:YES];
}];

关于presentViewController:animated:completion:的更多信息,请参考Apple文档

completion : 在展示完成后执行的块。 此块没有返回值且不带参数。您可以将此参数指定为nil。


好的,它将其推到模态框后面? - cdub
它将推入导航控制器堆栈。 - Kevin Machado
1
如果你想在之前关闭你的模态视图,你也可以使用dismissViewControllerAnimated:completion:并添加completionBlock。 - Kevin Machado
还有一个问题,一旦我关闭该帖子,该帖子的数据是否可以在ProfilesViewController中显示?因此,当它变得可见时,配置文件视图控制器是否运行? - cdub
当您关闭一个 ViewController 时,它会释放其中的所有数据。因此,您必须将想要保留的所有参数提供给新的 ViewController。希望这回答了您的问题。 - Kevin Machado

1
你的选项卡视图控制器是否嵌入了 UINavigationController? 如果没有,你当然不能使用 self.navigationController。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接