简体   繁体   English

如何在使用Storyboard解除模态视图控制器之前推送viewController?

[英]How to push a viewController before dismissing modal view controller with Storyboards?

While trying out storyboards for one of my projects I came across something for which I don't have a good solution; 在为我的一个项目试用故事板时,我遇到了一些我没有很好解决方案的东西;

I have a navigation-based application which shows a UITableViewController. 我有一个基于导航的应用程序,它显示了一个UITableViewController。 The tableView is populated with user-created elements. tableView使用用户创建的元素填充。 Tapping an element cell brings up the detail view controller. 点击元素单元格会调出详细视图控制器。 The user can create a new element by tapping a button in the tableView. 用户可以通过点击tableView中的按钮来创建新元素。 This brings up a modal view, which will handle the creation. 这将打开一个模态视图,它将处理创建。

Now, when a user is done with creating the element and dismisses the modal view controller, I want the user to see the corresponding new detail view controller and not the tableview. 现在,当用户完成创建元素并解除模态视图控制器时,我希望用户看到相应的新细节视图控制器而不是tableview。 But I can't figure out how to achieve this in storyboards. 但我无法弄清楚如何在故事板中实现这一目标。

Does anyone have a good pattern for this? 有没有人有这个好的模式?

Current situation 现在的情况

TableView --(tap create)--> creation modal view --(finish creating)--> TableView

Should be 应该

TableView --(tap create)--> creation modal view --(finish creating)--> detail view

You can put the creating view controller in a navigation controller and link the creation view controller to the detail view controller as well with a push segue. 您可以将创建视图控制器放在导航控制器中,并将创建视图控制器链接到详细视图控制器以及推送segue。 When you finish creating the data it will direct to an instance of detail view controller. 完成数据创建后,它将指向详细视图控制器的实例。

If you want to navigate back from details view directly to the table view, you can add a property to the details view controller, say @property (nonatomic) BOOL cameFromCreationViewController; 如果要从详细信息视图直接导航回到表视图,可以向详细信息视图控制器添加属性,例如@property (nonatomic) BOOL cameFromCreationViewController; . You can set this property in prepareForSegue: in the source view controller. 您可以在prepareForSegue:设置此属性prepareForSegue:在源视图控制器中。 In the details view make your own back button, and when it's tapped, you can do this: 在详细信息视图中,创建自己的后退按钮,当它被点击时,您可以执行以下操作:

if(self.cameFromCreationViewController){
     [self.presentingViewController dismissViewController];
}
else {
     [self.navigationController popViewController]
}

The best pattern I could come up with, is the same as the old pattern in code. 我能提出的最佳模式与代码中的旧模式相同。

Add a property (nonatomic, weak) UINavigationController *sourceNavigationController to the modal view controller. 将属性(nonatomic, weak) UINavigationController *sourceNavigationController到模态视图控制器。 When it becomes time to dismiss the modal view controller, add the following code: 当它成为解散模态视图控制器的时候,添加以下代码:

DetailViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailViewController"];
detailViewController.element = newlyCreatedElement;
[[self sourceNavigationController] pushViewController:detailViewController animated:NO];

And to make sure the sourceNavigationController get set properly, add the following code in the prepareForSegue: of the TableView: 并确保sourceNavigationController得到正确设置,添加在下面的代码prepareForSegue:在TableView中的:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"newElementSegue"]) {
        [[segue destinationViewController] setSourceNavigationController:self.navigationController];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM