I'm practicing awakeFromNib.
I have a single UIView in Main.storyboard.
This UIView inherit class CardView.
Codes are below
class ViewController: UIViewController {
@IBOutlet weak var cardView: CardView!
}
And I have a CardView.xib. In, CardView.xib, there's a default single UIView which inherits a class CardView. And inside this UIView, there's the other single view which inherits a class CardContentView.
And Of course, I have a CardView.swift which have class CardView.
Codes are below
class CardView: UIView {
@IBOutlet weak var cardContentView: CardContentView!
override func awakeFromNib() {
cardContentView.layer.cornerRadius = 16
}
}
And I have a CardContentView.xib. In, CardContentView.xib, there's a default single UIView which inherits a default class UIView.
And Of course, I have a CardContentView.swift which have class CardContentView.
Codes are below
class CardContentView: UIView {
@IBOutlet var backgroundView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit(){
Bundle.main.loadNibNamed("CardContentView", owner: self, options: nil)
self.addSubview(backgroundView)
}
}
Yes, I want to show the CardContentView to cardView in ViewController through class CardView.
But when I run, the error pops in class CardView
The error line is on the func awakeFromNib
The specific line is cardContentView.layer.cornerRadius = 16.
The error message is Unexpectedly found nil while unwrapping an Optional value. Especially, cardContentView = nil.
I don't know why cardContentView = nil.
I linked cardContentView in Xib board to class CardView in CardView.swift.
And how should I modify the code to run this?
Thank you!