One xib, several sub classes

前端 未结 1 1270
借酒劲吻你
借酒劲吻你 2021-01-21 12:22

I have one xib file for a custom UIView subclass. Works fine. I\'m able to load the correct nib and create an instance of my class and it contains all the subviews I added to th

相关标签:
1条回答
  • 2021-01-21 12:44

    You can try to write something really weird with -awakeAfterUsingCoder: to substitute created object, but this is really shaky and a few can get it right.

    The thing is that .xib file stores set of serialised objects, when this set is loaded, information about each object, i.e. it's class, size, other attributes, parent object, constraints are as well deserialised and applied. So, xib files store which class should receive +alloc and other messages and, consequently, which objects will then receive all the attributes via KVC (-setValue:forKey:). So, no, you can't just configure some class to load some xib, because xib file tells which class should be loaded.

    As a soulution I'd suggest to refactor your code, (for example) incapsulate different subclasses logic to some other object. So, before you had multiple subclasses with different logic, then, you'll have single class, loadable from xib, but you have to set some MyDifferentLogicVariant1Implamentor entity to preserve different logic for 'different' classes.

    Superclass - Subclass1 - Subclass2
    vs
    Superclass.differentLogic = DifferentLogicImplementor1
    Superclass.differentLogic = DifferentLogicImplementor2
    
    0 讨论(0)
提交回复
热议问题