I'm learning objective-c and cocoa touch at the same time as building an app so I've got a fairly simple question (I think), sorry I don't have enough brownie points on the site to show a visual diagram of my site.
But let me explain, I have the following
- Main ViewController
- ViewController A
- ViewController B
- ViewController C
- ViewController 1
- ViewController 2
ViewController A, B and C all have protocol delegates that is being used by Main ViewController.
ViewController 1 and 2 have protocol delegates that is being used by ViewController C.
However I also need ViewController C to be a delegate for Main ViewController.
Because of this I recently created a protocol delegate within Main ViewController, however when trying to assign ViewController C as the delegate I'm getting errors, more specially when trying to import the header file of Main ViewController into View Controller C I'm getting a failed compilation message for the following reasons
- Main ViewControllers header file can no longer find the protocol for ViewController C
- Expected specifier-qualifier-list before 'ViewController C'
Is what I am trying to accomplish possible? Can two view controllers bd delegates for one another? And if not what would be an idea way of accomplishing what I am doing?
Within ViewController C I tried an alternative, having a method to call Main ViewController by creating a pointer of the parent view controller, creating an instance of ViewController B (this is why I want the delegate to work) then adding ViewControler B's view to the new pointer I created, this compiles but doesn't work (I'll try and find out why, this isn't what I'm asking here).
Many thanks, I'm really appreciating this site for a while now
This smells like bad application design. But to resolve your compile error you simply do something like this:
By forward declaring
SomeDelegate
you do not have to include the header file where it is it fully declared. This is very useful for cases where two classes depend on each other.You will have to include the header file in the implementation source file. But that is usually not a problem.
Note they can be (as St3fan) described, but this is exactly why delegates should always be assigned without retaining the references, because otherwise each will keep the other from being dealloced.
With ARC you now use a weak reference for your delegates:
When the other view controller is de-allocated the reference becomes nil, and you don't need to track the other objects lifecycle manually.