2017-09-14 96 views
-5

這是我班的UIView:添加子類故事板崩潰

class overlap: UIView{ 
    init() { 
     super.init(frame: UIScreen.main.bounds) 
    } 
    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

它應該只是填補了屏幕。當我將故事板中的視圖添加到固定在屏幕邊緣的約束中時,它會在我啓動應用程序時崩潰。錯誤出現在上面的代碼中。

編程方式創建和加入子類與此初始化函數的工作原理:

override init(frame: CGRect) { 
    super.init(frame: frame) 
} 

但我想它通過故事板可重複使用,無需添加代碼。我的代碼有什麼問題,甚至可能我想要的是什麼?

謝謝!

+5

'fatalError(「init(coder :) has not been implemented」)' - 猜測實例化故事板時調用的是什麼。 – Kevin

回答

4

崩潰,當我啓動應用程序

因爲這就是你的代碼告訴它做的事。

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    fatalError("init(coder:) has not been implemented") 
} 

fatalError意味着「撞我」。