iOS8 Swift无法使用xib创建视图控制器

问题描述:

我正在使用Swift,并且正在修复iOS 8的一些bug。我使用.xib创建了一个视图控制器。例如,一个视图控制器和一个表格视图。iOS8 Swift无法使用xib创建视图控制器

这是我如何创建我的.xib文件。

1.I拖动的tableView到视图,enter image description here 2.将类文件的所有者,enter image description here 3.拖动以文件的所有者enter image description here 4.drag了的tableView到文件的所有者enter image description here,我有这样的代码在文件的所有者

class BicycleServiceInfoController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

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

    //other functions 
    ... 
} 

但是当我运行这个应用程式的iOS 8.4,并尝试提出这个观点控制器,它会崩溃。 Xcode说:“意外地发现零,同时解开可选值”。这是因为tableView在这段代码中是无效的。但它在iOS 9中运行良好。

有人能帮助我吗?

+2

,你如何创建视图 - 控制自己? – ogres

设置viewWillAppear函数中的代表。或者在界面生成器中。 iOS 8有一些比需要更晚的创建屏幕的问题。

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") 
} 

在iOS 8上,您需要调用此方法,在iOS 9或更高版本中,系统修复此错误。 你可以得到托南已经提供正确答案的答案here

+0

只是我,还是有太多的大括号?或者格式不好? – jww

+1

这是正确的答案。解决问题。感谢那! – OutOfBounds

,但大家谁斗争这个转换为雨燕2.3,在这里你去:

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") 
}