【问题标题】:Could not load NIB in bundle. Cocoa Pod project无法在捆绑包中加载 NIB。可可豆项目
【发布时间】:2017-10-03 13:43:11
【问题描述】:

我正在做一个简单的可可豆荚,它是一个自定义 UICollectionView。但我无法使用 UICollectionViewCell 的 xib 文件。我收到错误:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法在包中加载 NIB:“NSBundle(已加载)”,名称为“CWNumberedCollectionViewCell”。

import UIKit
class CWNumberedCollectionViewCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
}


import Foundation
import UIKit

public class CWCollectionViewNumbered: UICollectionView{

    let cellIdentifier = "CWCell"

    public init(frame: CGRect, layout: UICollectionViewFlowLayout, parent: UIView) {
        super.init(frame: frame, collectionViewLayout: layout)
        self.allowsMultipleSelection = true
        self.delegate = self
        self.dataSource = self
        self.register( UINib.init( nibName:"CWNumberedCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cellIdentifier)
        self.backgroundColor = UIColor.blue
    }

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

    override public func selectItem(at indexPath: IndexPath?, animated: Bool, scrollPosition: UICollectionViewScrollPosition) {
        print("tapped")
    }
}

I have added the xib file to copy bundle resource.

【问题讨论】:

    标签: ios swift cocoapods bundle xib


    【解决方案1】:

    xib 不在您要查找的模块中。您必须精确 xib 所在的当前模块包,例如使用let bundle = Bundle(for: self)。否则链接器将在主包中查找资源。

    这是我如何从 xib 文件加载图像的示例:

    class func loadImage(imageSize: ImageSize) throws -> UIImage {
        let bundle = Bundle(for: self)
        let imageName = self.imageName(imageSize)
        guard let image = UIImage(named: imageName, in: bundle, compatibleWith: .none) else {
            throw LoadingIndicatorError.missingImage
        }
        return image
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-07
      • 1970-01-01
      • 2017-11-01
      • 2016-11-21
      • 1970-01-01
      • 2012-03-28
      相关资源
      最近更新 更多