Skip to content

Instantly share code, notes, and snippets.

@chrisroff
Created August 25, 2016 15:01
Show Gist options
  • Save chrisroff/5fd56d160513a42d55732b0ab55b2c4d to your computer and use it in GitHub Desktop.
Save chrisroff/5fd56d160513a42d55732b0ab55b2c4d to your computer and use it in GitHub Desktop.
Dismissible & DismissalDelegate Protocols
import UIKit
protocol DismissalDelegate : class {
func finishedShowing(viewController: UIViewController)
}
protocol Dismissible : class {
weak var dismissalDelegate : DismissalDelegate? { get set }
}
extension DismissalDelegate where Self: UIViewController {
func finishedShowing(viewController: UIViewController) {
guard
viewController.isBeingPresented() &&
viewController.presentingViewController == self
else {
navigationController?.popViewControllerAnimated(true)
return
}
dismissViewControllerAnimated(true, completion: nil)
}
}
class PresentedController: UIViewController, Dismissible {
weak var dismissalDelegate: DismissalDelegate?
@IBAction func done() {
dismissalDelegate?.finishedShowing(self)
}
}
class PresentingViewController: UIViewController, DismissalDelegate {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let vc = segue.destinationViewController as? Dismissible {
vc.dismissalDelegate = self
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment