Wednesday, July 1, 2026
HomeiOS Developmentios - Tips on how to override xib as cumtom view?

ios – Tips on how to override xib as cumtom view?

[ad_1]

I’ve one xib file(SuperViewA) and tremendous class(SuperViewA).

class SuperViewA: UIView {
non-public let aaa: ...

   init(aaa: AAA) {
      self.aaa = aaa
      tremendous.init(body: .zero)
   }

   required init(coder: NSCoder) {
      tremendous.init(coder: coder)   // <- It is an error due to property aaa will not be initialized.
}

Then, I wish to subclass SuperViewA named SubViewA.

class SubViewA: SuperViewA {
...
}

SubViewA’s UI is simply similar with SuperViewA’s xib.
So, I needn’t make SubViewA.xib.
However I must override superclass’s initializer due to an initialized object from subclass is required within the superclass.

If I make SubViewA’s xib, SuperViewA’s UI(xib) will not be overriden. So, beneath code will likely be crashed.

let view = Bundle.major.loadNibNamed(String(describing: SubViewA.self), proprietor: nil, choices: nil)?.first as? SubViewA

How can I resolve this?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments