Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSViewController StoryboardInstantiatable
import Cocoa
protocol StoryboardInstantiatable: class {
/*
継承クラスで以下を定義してください
typealias StoryboardInstantiatableViewController = <#YourViewController#>
static var storyboardName: String {
return "<#Storyboard Name#>"
}
*/
associatedtype StoryboardInstantiatableViewController: NSViewController
/// Storyboard 名を返す
static var storyboardName: String {get}
/// Initial View Controller をロードするには nil を返す
static var storyboardID: String? {get}
static func fromStoryboard(withStoryboardID storyboardID: String?) -> StoryboardInstantiatableViewController
static func fromStoryboard() -> StoryboardInstantiatableViewController
}
extension StoryboardInstantiatable where Self: NSViewController {
static var storyboardName: String {
return "Main"
}
// デフォルト実装では View Controller 名を ID として返す
static var storyboardID: String? {
return "\(self)"
}
static func fromStoryboard() -> StoryboardInstantiatableViewController {
return fromStoryboard(withStoryboardID: self.storyboardID)
}
static func fromStoryboard(withStoryboardID storyboardID: String?) -> StoryboardInstantiatableViewController {
let sb = NSStoryboard(name: storyboardName, bundle: nil)
if let storyboardID = storyboardID {
return sb.instantiateController(withIdentifier: storyboardID) as! StoryboardInstantiatableViewController
}
else {
return sb.instantiateInitialController() as! StoryboardInstantiatableViewController
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.