Created
August 25, 2016 15:01
-
-
Save chrisroff/5fd56d160513a42d55732b0ab55b2c4d to your computer and use it in GitHub Desktop.
Dismissible & DismissalDelegate Protocols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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