如何在没有 viewcontroller 作为所有者的情况下将笔尖实例化到视图中?
How to instantiate a nib into view without a viewcontroller as owner?
我有一个 UICollectionViewCell 的子类,我想单独对其进行单元测试。我如何在没有所有者的情况下自行实例化它(这可能吗?)?
有可能。在此代码中,我假设您的单元格 class 名称为:CustomCollectionViewCell
并且它位于 CustomCollectionViewCell.xib
文件中:
let bundle = Bundle(for:CustomCollectionViewCell.self)
let nib = UINib(nibName: "CustomCollectionViewCell", bundle: bundle)
//nibName should be the same as your file name
let cell = nib.instantiate(withOwner: nil, options: nil).first as? CustomCollectionViewCell
我有一个 UICollectionViewCell 的子类,我想单独对其进行单元测试。我如何在没有所有者的情况下自行实例化它(这可能吗?)?
有可能。在此代码中,我假设您的单元格 class 名称为:CustomCollectionViewCell
并且它位于 CustomCollectionViewCell.xib
文件中:
let bundle = Bundle(for:CustomCollectionViewCell.self)
let nib = UINib(nibName: "CustomCollectionViewCell", bundle: bundle)
//nibName should be the same as your file name
let cell = nib.instantiate(withOwner: nil, options: nil).first as? CustomCollectionViewCell