简体   繁体   English

快速从助手/另一个类中关闭ViewController

[英]swift Dismiss ViewController from a helper / another class

I have a problem when i try to dismiss a view from a function of a helperClass instanced in the viewer class 我尝试从查看器类中实例化的helperClass的功能中消除视图时遇到问题

public func set(playerController: AVPlayerViewController){
playerController?.dismiss(animated: true, completion: nil)

whose view is not in the window hierarchy! 其视图不在窗口层次结构中!

how can I pass correctly the controller so the helper class can dismiss it? 如何正确传递控制器,以便助手类可以将其关闭?

Viewerclass: 查看者类别:

helper.add(player: player)
helper.set(playerController: playerController)

You should be able to just do dismiss(animated: true, completion: nil) from the presented view controller, as Apple libraries handle the dismissal both from presenter and the presented view controllers. 您应该能够从提供的视图控制器中执行dismiss(animated: true, completion: nil) ,因为Apple库可以处理来自主持人和提供的视图控制器的撤消。 No need to pass a reference 无需传递参考

You also can give a callback to dismiss something like this: 您还可以提供回调以消除以下内容:

helper.add(player: player) {
 self.dismiss(animated: true, completion: nil)
}

Player:
public func set(playerController: AVPlayerViewController, completion: (Void)->Void){
   completion()
}

Try like this from your helper class:- 在您的帮助程序类中尝试以下操作:-

AppDelegate.sharedInstance().window?.topMostController()?.dismiss(animated: true, completion: nil)

And add this function in your AppDelegate file :- 并将此功能添加到您的AppDelegate文件中:

class func sharedInstance() -> AppDelegate{
   return UIApplication.shared.delegate as! AppDelegate
}

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

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