views:

196

answers:

2

Hi all,

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

A: 

This smells like bad application design. But to resolve your compile error you simply do something like this:

@protocol SomeDelegate;

@interface FooThatUsesSomeDelegate : NSObject {
    id<SomeDelegate> delegate_;
}
@end

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.

St3fan
This is great, thank you. I'll read up on forward declaration. Now off topic, I don't expect any detailed answers in my app design, but just to clarify I'm building a simple game, the Main ViewController is swapping the a splash screen, and title screen, the View Controller C is the game view itself, with the game view holding multiple views. So View Controller C the game view will hold a bunch of var's and methods that have nothing to do with the Main ViewController, if possible could you briefly advise a more efficient model? I thought it wasn't bad but I'm now a little concerned!
Chris
I still need to get my head around how to use delegates, I quite understand the theory but I also realise I didn't need this solution anyway, as I can create a delegate method in ViewController C back to the Main ViewController to do what I want. Thanks again it's still good to have learnt something new
Chris
A: 

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.

Kendall Helmstetter Gelner