我无法将子类添加到我的父级UIView类。我正在尝试构建一个BOOK类,并有各种UIView和UIImageView类来构建封面和页面。将子类添加到SELF时出现错误。会喜欢一些见解。 PS - 总迅速小白在Swift中添加子类到UIView
//book
class bookview : UIView {
var cover: UIView!
var backcover: UIView!
var page: UIImageView!
init (pages: Int) {
//backcover cover
backcover = UIView(frame: CGRect(x: 200, y: 200, width: bookwidth, height: bookheight))
backcover.backgroundColor = UIColor.blue
self.addSubview(backcover) //ERROR HERE
//pages
for i in 0 ..< pages {
page = UIImageView(frame: CGRect(x: bookwidth * i/10, y: bookheight * i/10, width: bookwidth, height: bookheight))
page.backgroundColor = UIColor.red
self.addSubview(page) //ERROR HERE
}
//front cover
cover = UIView(frame: CGRect(x: 0, y: 0, width: bookwidth, height: bookheight))
cover.backgroundColor = UIColor.blue
self.addSubview(cover) //ERROR HERE
super.init(frame: CGRect(x: 0, y: 0, width: bookwidth, height: bookheight))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//add book
let book = bookview(pages: 3)
是什么错误之前说?我认为Xcode在这里很清楚。 – vacawama
self未初始化将'super.init(frame:CGRect(x:0,y:0,width:bookwidth,height:bookheight))'移动到init的第一行 – Cruz