Last active
August 29, 2015 14:24
-
-
Save danielctull/678f9413be42b3645d5b to your computer and use it in GitHub Desktop.
Use generics to get a child view controller of a given type
This file contains hidden or 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
| if let tabViewController = self.contentViewController?.childViewControllerWithType(NSTabViewController) { | |
| // tabViewController is an NSTabViewController, woop! | |
| } |
This file contains hidden or 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 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