Сценарий: Я пытаюсь работать с «динамическими» 1024 * представлениями, где подпредставление раскадровки ссылается на XIB.
Но у меня проблема с загрузкой определенного xib-файла, расположенного в основном (текущем) комплекте. Я думаю, что это будет прямолинейно.
Вот мой простой код:
import UIKit
@IBDesignable
class AccountInformationView: UIView, Nib {
let nibName = "AccountInformationView"
var contentView: UIView?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
guard let view = loadViewFromNib() else { return }
view.frame = bounds
addSubview(view)
contentView = view
}
func loadViewFromNib() -> UIView? {
// let bundle = Bundle(for: type(of: self))
let mainBundle = Bundle.main
let nib = UINib(nibName: nibName, bundle: mainBundle)
// check the following line due to crash:
_ = nib.instantiate(withOwner: self, options: nil).first as? AccountInformationView
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
}
Вылетает на линии:
nib.instantiate(withOwner: self, options: nil).first as? AccountInformationView
... причина: 'Не удалось загрузить NIB в комплекте:' NSBundle (загружен) 'с именем' AccountInformationView ''
Вот макет проекта:
Почему я не могу найти XIB?