无法使用 Xib 将 UIView 类型的值转换为 [CustomView] [英] Could not cast value of type UIView to [CustomView] using Xib
问题描述
我看过很多类似的问题,但很多都不是最新的,而且没有一个能解决我的问题.
I've seen a number of similar questions, but many aren't up-to-date, and none have fixed my issue.
我有一个自定义的 Xib 和类,CardView
.当我尝试在代码中实例化它时,我得到 Could not cast value of type 'UIView' to 'CardView'
.
I have a custom Xib and class, CardView
. When I try to instantiate it in code, I get Could not cast value of type 'UIView' to 'CardView'
.
类如下:
class CardView: NibView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var wordLabel: UILabel!
@IBOutlet weak var flipButton: UIButton!
@IBOutlet weak var playButton: UIButton!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed("CardView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
override func awakeFromNib() {
super.awakeFromNib()
}
}
和 NibView
是一个自定义超类,看起来像:
and NibView
is a custom superclass and looks like:
class NibView: UIView {
var view: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
// Setup view from .xib file
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Setup view from .xib file
xibSetup()
}
}
private extension NibView {
func xibSetup() {
backgroundColor = UIColor.white
view = loadNib()
// use bounds not frame or it'll be offset
view.frame = bounds
// Adding custom subview on top of our view
addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[childView]|",
options: [],
metrics: nil,
views: ["childView": view]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[childView]|",
options: [],
metrics: nil,
views: ["childView": view]))
}
}
extension UIView {
/** Loads instance from nib with the same name. */
func loadNib() -> UIView {
let bundle = Bundle(for: type(of: self))
let nibName = type(of: self).description().components(separatedBy: ".").last!
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as! UIView
}
}
这是我的文件检查器的屏幕截图,显示我将 File's Owner 自定义类设置为 CardView,这似乎是许多人的绊脚石
Here is a screenshot of my file inspector showing that I set the File's Owner custom class to CardView, which seems to be a stumbling block for many
最后,我尝试实例化 CardView
如下: CardView().loadNib() as!CardView
Finally, I try to instantiate the CardView
as follows: CardView().loadNib() as! CardView
推荐答案
如果有人仍然有这个问题,我也有同样的问题,我想我们遵循相同的教程.
In case someone still having this problem, I have the same problem and I think we follow same tutorial.
您在 xibSetup()
中调用了 loadNib()
.我想你不必再打电话了.
You have called loadNib()
in your xibSetup()
. I think you don't have to call it again.
所以而不是使用
let myCardView = CardView().loadNib() as! CardView
我只是用
let myCardView = CardView()
这篇关于无法使用 Xib 将 UIView 类型的值转换为 [CustomView]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!