简体   繁体   中英

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

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. 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 :-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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