[英]How do I create the init for a UIViewController subclass that I create programmatically in Swift?
說我的UIViewController
有兩個屬性, var animal: String
和var color: UIColor
。
如何創建自定義init
以便可以輕松地在代碼中創建它?
我希望有:
init(animal: String, color: UIColor) {
self.animal = animal
self.color = color
}
但是我收到一個編譯器錯誤,提示我未使用指定的初始化程序。 我在做錯什么/我應該怎么做對呢?
設置這些屬性后,調用super.init()
。 這應該夠了吧。 您的方法應如下所示:
init(animal: String, color: UIColor) {
self.animal = animal
self.color = color
super.init()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.