Skip to content

Instantly share code, notes, and snippets.

@danielctull
Last active August 29, 2015 14:24
Show Gist options
  • Save danielctull/678f9413be42b3645d5b to your computer and use it in GitHub Desktop.
Save danielctull/678f9413be42b3645d5b to your computer and use it in GitHub Desktop.
Use generics to get a child view controller of a given type
if let tabViewController = self.contentViewController?.childViewControllerWithType(NSTabViewController) {
// tabViewController is an NSTabViewController, woop!
}
import Cocoa
extension NSViewController {
func childViewControllerWithType<ViewController>(type: ViewController.Type) -> ViewController? {
if let vc = self as? ViewController {
return vc
}
for viewController in self.childViewControllers {
if let vc = viewController as? NSViewController {
if let child = vc.childViewControllerWithType(type) {
return child
}
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment