【发布时间】:2017-10-26 15:23:34
【问题描述】:
我试图了解如何从另一个类调用,例如 let creation = menuButtonController(image:#imageLiteral(resourceName: "maps")) 然后在 menuButtonController我设置这个:
class menuButtonController: UIView {
let buttonView = UIView()
let button = UIButton(type: .custom)
var imageButton: UIImageView
init(frame: CGRect, image: UIImage) {
super.init(frame: frame,image: image) //Cannot invoke UIView.init with an argument list of type (frame cgrect, image uiimage)
self.imageButton.image = image
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/* other functions */
}
它给了我一个我无法解决的错误。谁能帮帮我?
编辑
现在如果我只想用(image: UIImage) 调用初始化程序,我应该像这样使用convenience init 吗?
convenience init(image: UIImage) {
self.init(image: image)
self.imageButton.image = image
}
当我尝试运行应用程序时它不会给我错误但会崩溃
【问题讨论】:
-
你能分享你得到的错误是什么吗?
-
我把它写在代码里面的注释里。这是一个副本:不能使用类型参数列表调用 UIView.init (frame cgrect, image uiimage)
-
用
xxxController形式的名称命名UIView的子类是一个糟糕的想法。视图对象和控制器对象是完全不同的动物,名为xxxController的对象应该是控制器对象,而不是视图对象。哦,类名应该以大写字母开头。所以你的班级应该命名为MenuButtonView
标签: ios swift init initializer