开发者

Cannot access appdelegate instance variable from related View Controller

开发者 https://www.devze.com 2023-04-02 06:19 出处:网络
My app name is PropertiesSearch so the project contains the following files: PropertiesSearchAppDelegate.h

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号