简体   繁体   English

使用Swift 4解除iOS中的所有模态

[英]Dismiss all modals in iOS with Swift 4

I am trying to achieve a navigation similar to the Netflix app for iOS. 我正在尝试实现类似于iOS的Netflix应用程序的导航。 When you click on a movie, a modal window pops up with a close button. 单击电影时,会弹出一个模式窗口,其中包含关闭按钮。 If within this movie I choose to see another movie then the second modal pops up and in addition to the close button, a back button appears. 如果在这部电影中我选择看另一部电影,则弹出第二个模态,除关闭按钮外,还会出现一个后退按钮。 I can use the back button to dismiss one by one and the close button to return to the base screen. 我可以使用后退按钮逐一关闭,关闭按钮返回基本画面。

I am able to dismiss a single view using 我可以使用解雇单个视图

dismiss(animated: true, completion: nil)

but how can I return to the base screen closing all modals at once? 但是我怎样才能回到基本画面一次关闭所有模态? Also, is modals the way to go? 另外,模态是要走的路吗? I chose this because I didn't want the navigation bar on top. 我之所以选择这个,是因为我不希望导航栏位于顶部。

I'm working with Swift 4.2 in Xcode 10. 我在Xcode 10中使用Swift 4.2。

The way you are dismissing a ViewController is not the correct way. 你解雇ViewController的方式不正确。 The presenting view controller is responsible for dismissing the view controller. 呈现视图控制器负责解除视图控制器。 Ideally you have to implement a protocol in your presenting ViewController and , dismiss your modal from your 'presenting' ViewController not 'presented' ViewController. 理想情况下,你必须在你的呈现ViewController中实现一个协议,并从你的'呈现'ViewController中解除你的模态而不是'呈现'的ViewController。

The reason why your way still works is, when a ViewController calls self.dimiss if there's nothing to dismiss UIKit will delegate it back to its parent. 你的方式仍然有效的原因是,当一个ViewController调用self.dimiss如果没有什么可以解雇的话,UIKit会将它委托给它的父节点。 If you implement this correct way, once you dismiss , your presenting viewcontroller will dismiss , hence all the presented viewcontrollers will be dismissed instead of the last one. 如果你实现这种正确的方式,一旦你解雇,你的呈现视图控制器将被解雇,因此所有呈现的视图控制器将被解雇而不是最后一个。

From Apple Docs: 来自Apple Docs:

The presenting view controller is responsible for dismissing the view controller it presented. 呈现视图控制器负责解除它所呈现的视图控制器。 If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal. 如果在呈现的视图控制器本身上调用此方法,UIKit会要求呈现视图控制器处理解雇。

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. 如果连续呈现多个视图控制器,从而构建一堆呈现的视图控制器,则在堆栈中较低的视图控制器上调用此方法会解除其直接子视图控制器以及堆栈上该子视图上方的所有视图控制器。 When this happens, only the top-most view is dismissed in an animated fashion; 发生这种情况时,只有最顶层的视图以动画方式被删除; any intermediate view controllers are simply removed from the stack. 任何中间视图控制器都可以从堆栈中删除。 The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack. 最顶层的视图使用其模态过渡样式被忽略,这可能与堆栈中较低的其他视图控制器使用的样式不同。

If you want to retain a reference to the view controller's presented view controller, get the value in the presentedViewController property before calling this method. 如果要保留对视图控制器呈现的视图控制器的引用,请在调用此方法之前获取presentViewController属性中的值。

The completion handler is called after the viewDidDisappear(_:) method is called on the presented view controller. 在呈现的视图控制器上调用viewDidDisappear(_ :)方法之后调用完成处理程序。

试试这个

 self.navigationController?.viewControllers.removeAll(where: {$0.isModalInPopover})

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

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