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 the xib file.
However, I have also subclassed this view, but I can't figure out how to create an instance of this class and get it to use the xib file used by the parent class. Is this even possible? I don't want to create a new xib file for my subclass, since the view hierarchy, subviews and GUI looks the same, it's just the code that differs.
Can I load a nib and "connect" it to another class than the one specified as the "Custom class" in the xib settings? Or can I create a new instance of a view and tell it to use a xib of a certain name?
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.