使用 Swift 4 关闭 iOS 中的所有模态
Dismiss all modals in iOS with Swift 4
我正在尝试为 iOS 实现类似于 Netflix 应用程序的导航。当您单击电影时,会弹出带有关闭按钮的模态 window。如果在这部电影中我选择看另一部电影,则会弹出第二个模式,除了关闭按钮外,还会出现一个后退按钮。我可以使用后退按钮逐一关闭,然后使用关闭按钮 return 返回基本屏幕。
我可以使用
关闭单个视图
dismiss(animated: true, completion: nil)
但是我怎样才能return到基本屏幕一次关闭所有模式?另外,模式是要走的路吗?我选择这个是因为我不想让导航栏在顶部。
我在 Xcode 10 中使用 Swift 4.2。
试试这个
self.navigationController?.viewControllers.removeAll(where: {[=10=].isModalInPopover})
您关闭 ViewController 的方式不正确。呈现视图控制器负责关闭视图控制器。理想情况下,您必须在呈现 ViewController 时实施一个协议,并且从 'presenting' ViewController 而不是 'presented' ViewController.
中关闭您的模态
你的方法仍然有效的原因是,当 ViewController 调用 self.dimiss
时,如果没有什么可以关闭,UIKit 会将它委托回其父级。如果您实施这种正确的方式,一旦您 dismiss ,您的呈现 viewcontroller 将被 dismiss ,因此所有呈现的 viewcontrollers 将被 dismissed 而不是最后一个。
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.
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.
The completion handler is called after the viewDidDisappear(_:) method is called on the presented view controller.
我正在尝试为 iOS 实现类似于 Netflix 应用程序的导航。当您单击电影时,会弹出带有关闭按钮的模态 window。如果在这部电影中我选择看另一部电影,则会弹出第二个模式,除了关闭按钮外,还会出现一个后退按钮。我可以使用后退按钮逐一关闭,然后使用关闭按钮 return 返回基本屏幕。
我可以使用
关闭单个视图dismiss(animated: true, completion: nil)
但是我怎样才能return到基本屏幕一次关闭所有模式?另外,模式是要走的路吗?我选择这个是因为我不想让导航栏在顶部。
我在 Xcode 10 中使用 Swift 4.2。
试试这个
self.navigationController?.viewControllers.removeAll(where: {[=10=].isModalInPopover})
您关闭 ViewController 的方式不正确。呈现视图控制器负责关闭视图控制器。理想情况下,您必须在呈现 ViewController 时实施一个协议,并且从 'presenting' ViewController 而不是 'presented' ViewController.
中关闭您的模态你的方法仍然有效的原因是,当 ViewController 调用 self.dimiss
时,如果没有什么可以关闭,UIKit 会将它委托回其父级。如果您实施这种正确的方式,一旦您 dismiss ,您的呈现 viewcontroller 将被 dismiss ,因此所有呈现的 viewcontrollers 将被 dismissed 而不是最后一个。
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.
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.
The completion handler is called after the viewDidDisappear(_:) method is called on the presented view controller.