Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

ios - Adding subclass to Storyboard crashes

This is my class of a 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")
    }
}

It should just fill up the screen. When I add the view in Storyboard with constraints pinned to the edges of the screen, it crashes when I launch the app. The error comes up as stated in the code above.

Creating and adding the subclass programmatically works with this init function:

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

But I want it reusable through Storyboard, without adding code. What is wrong with my code, and is it even possible what I want?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

it crashes when I launch the app

Because that is what your code told it to do.

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

fatalError means "crash me".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...