菜鸟教程小白 发表于 2022-12-11 19:47:27

ios - 无法在 bundle Xcode8 中加载 NIB


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

<p>这是我的代码:</p>

<pre><code>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() -&gt; 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) as! UIView
    return view
}

}
</code></pre>

<p>有人知道如何解决这个问题吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您要从 Nib 加载 View ,我认为最好的简单方法是:</p>

<pre><code>   var yourView : YourView =NSBundle.mainBundle().loadNibNamed(&#34;myXib&#34;, owner: self, options: nil) as! YourView
</code></pre>

<p>希望对你有帮助</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法在 bundleXcode8 中加载 NIB,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44296366/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44296366/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法在 bundle Xcode8 中加载 NIB