-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Description
Hi, as I understand the purpose of awakeFromNib() is that is should me called once after awaking from nib. The downside of using awakeAfter(using: aDecoder) is that the method is called twice: first time where view is awaken from e.g. UIViewController and second time when our view is awaken from the .xib
I have managed to find a workaround by checking if superview == nil. It equals nil when awaking from UIVIewController and superview is not set yet. When awake from .xib is called due awakeAfter(using:) then the superview is not nil
@IBDesignable
open class MyNibView: UIView, NibLoadable {
open class var nibName: String {
return String(describing: self)
}
open override func awakeAfter(using aDecoder: NSCoder) -> Any? {
return nibLoader.awakeAfter(using: aDecoder, super.awakeAfter(using: aDecoder))
}
open override func awakeFromNib() {
// this will fire twice
guard self.superview == nil else { return }
self.awakeFromNibOnce()
}
open func awakeFromNibOnce() {
// this will fire once
}
}
My question is: can we manage to do it without the workaround?
Metadata
Metadata
Assignees
Labels
No labels