James White
James White

Reputation: 784

Reference a view's parent without getting a circular import

I have a custom UIView class which needs to know about its parent (another different custom UIView class).

  1. The parent class has to import the header of the child class, so it can add subviews of that class.
  2. 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.
  3. If I do this, I get circular import issues.

If anyone can make any sense of this, can you help to resolve this? Thanks.

Upvotes: 0

Views: 728

Answers (3)

JRG-Developer
JRG-Developer

Reputation: 12663

While you could use a forward declaration (@class ParentClass) and a weak reference to the parent (@property (nonatomic, weak) ParentClass *parent) in the child's header file, this is generally not a good programming practice.

Reasons why this is generally not a good idea:

1) As the project gets bigger, you're likely going to violate DRY ("don't repeat yourself") as the child necessitates a parent of a certain class... what if another parent later needs to create the same child object? You'd have to create a new class that declares another forward class of the new parent and has a weak property to it.

2) This is also likely going to lead to spaghetti code... what if you want to add a new feature to the parent that affects a method the child is using? Do you create a new yet similar method that's slightly different (see point 1 about violating DRY)? Do you create an input to the original method (you'd also have to make sure that the child now knows about this change and passes the appropriate input).

Instead, the Delegation design pattern works better here. Apple also frequently uses this throughout their libraries. In example, UITableView declares a delegate and a datasource so that it can delegate actions (clicks on cells) and data input (creation of custom cells) to other owning classes, without the UITableView object having to know about the implementation of said parent class.

For more information on the Delegation pattern in general, see Wikipedia on it here:

http://en.wikipedia.org/wiki/Delegation_pattern

For a tutorial on creating your own protocols (how delegation is implemented in iOS), see this tutorial here:

http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-custom-delegates/

For high quality tutorials and introductions on iOS in general, including delegation and other necessary iOS concepts, see Ray Wenderlich's site also here:

http://www.raywenderlich.com/

Good luck!

Upvotes: 0

Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21966

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:

@property(nonatomic,weak) id child;

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.

Upvotes: 0

dmaij
dmaij

Reputation: 1007

What you want is commonly known as a forward declaration.

refer to Objective-C: Forward Class Declaration for more information

Upvotes: 2

Related Questions