Hongxu Jin
Hongxu Jin

Reputation: 807

iOS8 Swift can't create view controller with xib

I'm using Swift and I'm fixing some bug of iOS 8. I created a view controller with .xib. For example, a view controller and a table view in it.

This how I created my .xib file.

1.I drag a tableView into the view,enter image description here 2.set the class of the file's owner,enter image description here 3.drag the view to file's ownerenter image description here 4.drag the tableView to the file's ownerenter image description here And I have this code in file's owner

class BicycleServiceInfoController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutLet tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

    //other functions
    ...
}

But when I run this APP in iOS 8.4 and try to present this view controller, It will crash. Xcode says "unexpectedly found nil while unwrapping an Optional value". It's because tableView is nil in this code. But it works well in iOS 9.

Someone can help me?

Upvotes: 1

Views: 660

Answers (3)

Alexander Gattringer
Alexander Gattringer

Reputation: 175

Tonin already provided the right answer but for everyone who struggles converting this to Swift 2.3, here you go:

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    let classString = String(MyClassName)
    if NSBundle.mainBundle().pathForResource(classString, ofType: "nib") == nil{
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    } else {
        super.init(nibName: nibNameOrNil ?? classString, bundle: nibBundleOrNil)
    }
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Upvotes: 1

Tonin
Tonin

Reputation: 131

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    let classString = String(describing: type(of: self))
    if Bundle.main.path(forResource: classString, ofType: "nib") == nil {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    } else {
        super.init(nibName: nibNameOrNil ?? classString, bundle: nibBundleOrNil)
    }
}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

On iOS 8, you need call this method, on iOS 9 or later, system fixes this bug. You can get the answer here

Upvotes: 2

Max Pevsner
Max Pevsner

Reputation: 4094

Set the delegates in the viewWillAppear function. Or in the interface builder. iOS 8 has some issues of creating screens later than needed.

Upvotes: 0

Related Questions