Sk8eR
Sk8eR

Reputation: 63

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)

Upvotes: 1

Views: 948

Answers (3)

Himanshu
Himanshu

Reputation: 2832

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
}

Upvotes: 0

Auryn
Auryn

Reputation: 1176

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()
}

Upvotes: 1

Alistra
Alistra

Reputation: 5195

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

Upvotes: 3

Related Questions