Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save makthrow/24cace291b7c4120f6aa to your computer and use it in GitHub Desktop.
Save makthrow/24cace291b7c4120f6aa to your computer and use it in GitHub Desktop.
Extension to UIViewController for easy handling of navigationControllers that are destinationViewControllers
// as described in CS193P
// If i'm a navigationController, then return my visible View Controller. otherwise, return myself
// now i can send to any viewcontroller, give me the content view controller. if you are the content view controller,
// you give me yourself. if you are a nav controller, you give me the visible view controller
extension UIViewController {
var contentViewController: UIViewController {
if let navcon = self as? UINavigationController {
return navcon.visibleViewController
}
else {
return self
}
}
}
// example
override func prepareForSegue() {
if let ivc = segue.destinationViewController.contentViewController as? ImageViewController {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment