Can't Load UIViewController XIB file in Storyboard in Swift

后端 未结 2 1526
轻奢々
轻奢々 2020-12-02 23:37

I\'ve read Using XCode storyboard to instantiate view controller that uses XIB for its design but I\'m having troubles making this work in Swift (Using Xcode 6 Beta 6). I\'m

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 00:08

    Another answer is:

    override func loadView() {
        if #available(iOS 9, *) {
            super.loadView()
        } else {
            let classString = String(self.dynamicType)
            NSBundle.mainBundle().loadNibNamed(classString, owner: self, options: nil)
        }
    }
    

    link: http://japko.net/2014/09/08/loading-swift-uiviewcontroller-from-xib-in-storyboard/

    EDIT:

    override var nibName: String? {
        get {
            let classString = String(self.dynamicType)
            return classString
        }
    }
    override var nibBundle: NSBundle? {
        get {
            return NSBundle.mainBundle()
        }
    }
    

    This looks more beautiful. Works on iOS 8/9 (maybe on iOS 7 too, who knows...))

提交回复
热议问题