I have a custom UIView class which needs to know about its parent (another different custom UIView class).
- The parent class has to import the header of the child class, so it can add subviews of that class.
- The child class has to import the header of the parent class, so it can access its methods and properties. It has to do the import in its .h file rather than its .m, because I need to make the child’s parent an instance variable.
- If I do this, I get circular import issues.
If anyone can make any sense of this, can you help to resolve this? Thanks.
There are many ways to solve this, for example by just declaring the reference to the other class as an id, and to send forward messages to it (in Objective-C you don’t even need to cast them, the compiler wouldn’t complain about that).
For example:
But you may review your design in a way that you use a root controller that handles both the classes. This way A doesn’t directly speak to B and B doesn’t directly speak to A. Instead if A wants to speak with B, speaks with C and C speaks with B, and viceversa.