简体   繁体   English

是否可以快速从模态视图选择到导航控制器上不在层次结构中的视图?

[英]Is it possible to segue from a modal view to a view that is not in the hierarchy on a Navigation Controller in swift?

On my main view in a NavigationController I have a button that brings up a new view modally to submit a post. 在我的NavigationController的主视图中,我有一个按钮,该按钮以模态弹出一个新视图以提交帖子。 I have a button to dismiss this view which brings the user back to the main view, however if the users proceeds to make a post, I want it to dismiss the view and proceed to a show the post in the Navigation controller. 我有一个按钮可以关闭该视图,从而使用户回到主视图,但是,如果用户继续发布帖子,则希望它关闭该视图并继续在导航控制器中显示该帖子。

The ideal effect would have the standard navigation controller's back button on the top that can bring the user back to the main view when they have finished looking at their post. 理想效果是在顶部具有标准导航控制器的后退按钮,当用户查看完帖子后,可以将其带回到主视图。

I have tried several different methods, but I tend to get an error stating "Warning: Attempt to present (The View I Want to Show) whose view is not in the window hierarchy! 我尝试了几种不同的方法,但是我趋于得到一个错误,指出“警告:尝试呈现(我想要显示的视图)其视图不在窗口层次结构中!

Thanks for any info! 感谢您提供任何信息!

Yes, it's possible. 是的,有可能。 This is the best way I've found to structure the app to allow this functionality. 这是我发现构建应用程序以允许该功能的最佳方法。

  • RootViewController RootViewController
    • UINavigationViewController UINavigationViewController
      • ContentViewController (your current "main" view controller) ContentViewController(您当前的“主”视图控制器)

You're basically going to create a new RootViewController that is going to contain your current UINavigationController as a child. 基本上,您将创建一个新的RootViewController,它将包含当前的UINavigationController作为子级。 I usually setup the window.rootViewController programmatically in the AppDelegate. 我通常以编程方式在AppDelegate中设置window.rootViewController。 I would also keep a global reference to it. 我还将在全球范围内引用它。

Have a look at Apple's documentation on this. 看一下苹果的文档 Basically, the RootViewController code would look like this: 基本上,RootViewController代码如下所示:

[self addChildViewController:navController];
navController.view.frame = self.view.bounds
[self.view addSubview:self.navController.view];
[navController didMoveToParentViewController:self];

Now, whenever you need to present a modal view controller, present it from the RootViewController, instead of the current/top view controller on the UINavigationBar. 现在,每当需要显示模式视图控制器时,都可以从RootViewController而不是UINavigationBar上的当前/顶部视图控制器显示它。 This allows you to manipulate the UINavigtaionController independently and you won't get the error you're seeing. 这样,您就可以独立地操作UINavigtaionController,并且不会看到错误。

So present the modal view controller from the RootViewController, this covers the UINavigationController and its content (top view controller). 因此,从RootViewController呈现模态视图控制器,其中涵盖了UINavigationController及其内容(顶视图控制器)。 Make any changes you need to the UINavigationController stack (push, pop, etc...) with no animation. 在没有动画的情况下,对UINavigationController堆栈进行任何所需的更改(推,弹出等)。 When you're done dismiss the modal view controller (with animation) and it will show you're adjusted UINavigationController. 完成后,关闭模态视图控制器(带有动画),它将显示您已调整UINavigationController。

Hopefully, this all makes sense. 希望这一切都有道理。 It's kind of complex and hard to explain in text. 这有点复杂,很难用文字解释。 :p :p

If you don't want to setup a RootViewController, you might want to try presenting the modal view controller from the UINavigationController, as that might have the same effect. 如果您不想设置RootViewController,则可能要尝试通过UINavigationController呈现模式视图控制器,因为这可能具有相同的效果。 From the view controller just do self.parentViewController.present. 在视图控制器中执行self.parentViewController.present。

The main thing you're trying to avoid is presenting a modal from a view controller and then removing that view controller (pop) before the modal is dismissed. 您要避免的主要事情是从视图控制器中呈现一个模态,然后在消除模态之前删除该视图控制器(pop)。 The view controller that presents the view controller is responsible for dismissing it. 提供视图控制器的视图控制器负责将其关闭。

I finally got it all working (I'm still new to Swift) and the two comments on the question were very helpful. 我终于使一切正常(我还是Swift的新手),关于该问题的两条评论非常有帮助。 The most helpful of all was the top answer here's example on github. 最有用的是在github上的示例的最高答案。 Similar question 类似问题

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

相关问题 从导航控制器到视图控制器的Swift自定义展开Segue - Swift custom unwind Segue from Navigation controller to View Controller 手动模态segue不起作用,View Controller不在窗口层次结构中? - Manual modal segue does not work, View Controller is not in the window hierarchy? 使用导航控制器进行Segue查看 - Segue to view with Navigation Controller 自定义到导航控制器层次结构中特定子视图的序列 - Custom segue to specific sub-view in navigation controller hierarchy 如何从根视图控制器到导航堆栈中的最后一个视图控制器快速执行segue? - how to perform segue from root view controller to the last view controller in the navigation stack in swift? 从modal segue Swift访问父视图 - Accessing parent view from modal segue Swift 从tableview看到导航控制器之前的视图 - Segue from tableview to view preceded by navigation controller 导航栏下的模态视图控制器-Swift - Modal view controller under Navigation Bar - Swift 具有透明视图的模态Segue到导航控制器变为灰色 - Modal Segue To Navigation Controller With Transparent View Turns Grey 模态Segue View Controller的保存 - Modal Segue View Controller Preservation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM