簡體   English   中英

如何為我在Swift中以編程方式創建的UIViewController子類創建初始化?

[英]How do I create the init for a UIViewController subclass that I create programmatically in Swift?

說我的UIViewController有兩個屬性, var animal: Stringvar 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM