swift 解雇一个助手/另一个 class ViewController

swift Dismiss ViewController from a helper / another class

当我尝试从查看器中实例化的 helperClass 的函数中关闭视图时遇到问题 class

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

whose view is not in the window hierarchy!

我怎样才能正确传递控制器,以便助手 class 可以关闭它?

观看者class:

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

您应该能够从呈现的视图控制器中执行 dismiss(animated: true, completion: nil),因为 Apple 库处理来自呈现器和呈现的视图控制器的解雇。无需传递引用

从你的助手那里尝试这样 class:-

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

并在您的 AppDelegate 文件中添加此函数:-

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

您也可以回拨取消 像这样:

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

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