【发布时间】:2016-11-26 18:06:38
【问题描述】:
我无法向我的父 UIView 类添加子类。我正在尝试构建一个 BOOK 类并使用各种 UIView 和 UIImageView 类来构建封面和页面。将子类添加到 SELF 时出现错误。会喜欢一些见解。 PS - 总迅速的菜鸟
//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 在这里很清楚。
-
self 未初始化移动
super.init(frame: CGRect(x: 0, y: 0, width: bookwidth, height: bookheight))到 init 的第一行
标签: ios swift uiview swift-playground