views:

223

answers:

2

I'm going to use an example to properly illustrate my confusion. I can't quite wrap my head around this.

In Cocoa touch, we have UIViewController and its subclass, UINavigationController. Now, UIVC has an ivar of type UINav, and to get around the circular import problem they use @class UINavigationController. I am assuming they then #import "UINavigationController in UIViewController.m (or somesuch).

My confusion is thus: How do subclasses of UIViewController then know about the methods declared in UINavigationController? From inside a UIViewController subclass one might call [self.navigationController popViewController], but how is that method known?

The only idea I have is UINavigationController must be separately imported to every subclass (perhaps in the prefix?)

Any ideas?

+1  A: 

If this project was created with one of the Xcode templates, then the headers for all the classes in the UIKit are probably being included from the pre-compiled header of the project.

NSResponder
After thinking about it more, I've realized this is true. It's not really anything being done by the compiler, things are being imported, it's just hidden away inside other headers and the prefix. Thanks!
jbrennan
There is no mass importing of pre-complied headers. The @class directive causes the compiler to search forward to find the header defined in the implementation file. You can see this for yourself by creating classes with circular references. See my example in my answer.
TechZen
"There is no mass importing of pre-complied headers. "Look up "project headers" in the Xcode docs. The question was about UIKit classes; your example does not apply.
NSResponder
A: 
TechZen
“Subclasses inherent [sic] the headers imported in their super classes implementation file.” No, they don't. A subclass inherits the superclass's ivars, and a method implementation in a subclass can call a method implementation in its immediate superclass. That's all the inheritance there is. Try this: Define a macro in ClassA.h, then try to use that macro in ClassC.m (without importing ClassA.h there). It won't compile.
Peter Hosey