我创建了一个可以在多个地方重用的自定义视图,我已经将该类名称分配给了视图控制器的子视图。这里我完全使用了XIB。在运行该应用程序时,它崩溃了,并遇到了错误“线程1:“ Bundle.main.loadNibNamed(” TripCancelAlert“,所有者:self,选项:nil)”处的EXC_BAD_ACCESS(code = 2,address = 0x7ffeee23be2c)“
查看课程:
array(['0', '+10:00', '+08:00', '+11:00', '+08:00', '+09:00'],
dtype=object)
答案 0 :(得分:1)
在超类下方找到您的视图。
请确保使用与xib
(class
)相同的名称来命名MyView.xib / MyView
class NibView: UIView {
@IBOutlet var view: UIView!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
guard let viewFromNib = Bundle.main.loadNibNamed(className, owner: self, options: nil)?.first as? UIView
else { fatalError("Could not load view from nib file.") }
view = viewFromNib
translatesAutoresizingMaskIntoConstraints = false
view.translatesAutoresizingMaskIntoConstraints = false
addPinnedSubview(view)
}
func addPinnedSubview(_ view: UIView, withInsets insets: UIEdgeInsets = .zero) {
addSubview(view)
let viewsDict: [String: UIView] = ["childView": view]
var metrics: [String: Any] = [:]
metrics["topSpace"] = insets.top
metrics["bottomSpace"] = insets.bottom
metrics["leftSpace"] = insets.left
metrics["rightSpace"] = insets.right
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(topSpace)-[childView]-(bottomSpace)-|",
options: [],
metrics: metrics,
views: viewsDict))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(leftSpace)-[childView]-(rightSpace)-|",
options: [],
metrics: metrics,
views: viewsDict))
}
}