Cannot access appdelegate instance variable from related View Controller
My app name is PropertiesSearch so the project contains the following files:
PropertiesSearchAppDelegate.h
PropertiesSearchAppDelegate.m
PropertiesSearchViewController.h
PropertiesSearchViewController.m
I declared an instance variable (ListingNav) in PropertiesSearchAppDelegate开发者_开发百科.h like this:
#import <UIKit/UIKit.h>
@class PropertiesSearchViewController;
@interface PropertiesSearchAppDelegate : NSObject <UIApplicationDelegate> {
UINavigationController *ListingNav;
}
@property (nonatomic, retain) UINavigationController *ListingNav;
@end
and in PropertiesSearchAppDelegate.m, I added
@synthesize ListingNav;
Now I'm trying to access ListingNav from PropertiesSearchViewController.m like this:
PropertiesSearchAppDelegate *appDelegate = (PropertiesSearchAppDelegate *)[[UIApplication sharedApplication] delegate];
// Then here I can easily do appDelegate.ListingNav etc...
But debugger is showing:
Use of undeclared identifier PropertiesSearchAppDelegate
Should I import PropertiesSearchAppDelegate.h in PropertiesSearchViewController.h ?
Thx in advance for your help
Stephane
You have to import PropertiesSearchAppDelegate.h in PropertiesSearchViewController.h.
To let a class know about another class, you should either import the class or add @class directive. Adding @class directive just lets the class aware that another class exists. In your case, you should import PropertiesSearchAppDelegate as you are trying to access the members of the class.
精彩评论