0
我是一個快速的新手。我正在嘗試構建動態對象。在我的例子中,我試圖從我的BOOK類創建一個PAGE類的實例。我想我不知道如何動態創建它們/命名它們或引用它們以備後用。我知道我不能創建動態名稱......我猜我把它們放到了棕褐色數組中?任何幫助或輸入讚賞。在Swift 3中創建動態對象
//page class
class pageview : UIView {
var page: UIView!
init() {
super.init(frame: CGRect(x: 0, y: 0, width: bookwidth, height: bookheight))
page = UIView(frame: CGRect(x: bookposx, y: bookposy, width: bookwidth, height: bookheight))
page.backgroundColor = UIColor.blue
book.addSubview(page)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//book class
class bookview : UIView {
var cover: UIView!
var backcover: UIView!
var page: UIImageView!
init (numberofpages: Int) {
super.init(frame: CGRect(x: 0, y: 0, width: bookwidth, height: bookheight))
//pages
for i in 0 ..< numberofpages {
//PROBLEM HERE
let page (i) = pageview()
self.addSubview(page (i))
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//add book
let book = bookview(numberofpages: 3)
container.addSubview(book)
儘管此代碼可以回答這個問題,提供有關附加的上下文*爲什麼*和/或*如何*這個代碼回答了這個問題提高了其長期價值。 http://stackoverflow.com/help/how-to-answer – jtbandes