OGeek|极客世界-中国程序员成长平台

标题: ios - 无法在 bundle Xcode8 中加载 NIB [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:47
标题: ios - 无法在 bundle Xcode8 中加载 NIB

我遵循了一个教程,但是当我尝试运行我的代码时它崩溃了,因为它无法在包中加载 NIB。

这是我的代码:

class BuyPremiumView: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)
    setupView()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupView()
}

// MARK: - Private Helper Methods

// Performs the initial setup.
fileprivate func setupView() {
    let view = viewFromNibForClass()
    view.frame = bounds
    view.autoresizingMask = [
        UIViewAutoresizing.flexibleWidth,
        UIViewAutoresizing.flexibleHeight
    ]
    addSubview(view)
}

// Loads a XIB file into a view and returns this view.
fileprivate func viewFromNibForClass() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
    return view
}

}

有人知道如何解决这个问题吗?



Best Answer-推荐答案


如果您要从 Nib 加载 View ,我认为最好的简单方法是:

   var yourView : YourView =  NSBundle.mainBundle().loadNibNamed("myXib", owner: self, options: nil)[0] as! YourView

希望对你有帮助

关于ios - 无法在 bundle Xcode8 中加载 NIB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44296366/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4