我有一个继承自 UIView 的 BaseView 类:
@IBDesignable class BaseView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
我在 Storyboard中为 UILabel
、UIButton
和 UITextField
设置了此类。
现在,我必须认识到 UILabel
、UIButton
或 UITextField
中的哪一个被称为 init?(coder aDecoder: NSCoder)
.
有什么办法解决这个问题吗?
更新:
似乎无法将 UIView 类型的自定义类设置为 UILabel,因此无法解决此问题。
最佳答案
我试着测试这个类
if self is UILabel { }
但是我收到一个编译器警告,从 BaseView 转换到不相关的 UILabel 总是失败
if (self as UIView) is UILabel { }
清除警告但没有用。
那么一个问题:你说“我在 Storyboard中为 UILabel、UIButton 和 UITextField 设置了这个类”
你能详细说明你做了什么吗?
如何声明 IBOutlet ?基础 View ?您在 IB 中将什么类型分配给标签? 因为 BaseView 不是 UILabel 的子类,所以我无法将标签声明为 BaseView ?
关于ios - 如何找到调用解码器初始化程序的基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47495010/