我無法將子類添加到我的父級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