【问题标题】:Cannot access appdelegate instance variable from related View Controller无法从相关的 View Controller 访问 appdelegate 实例变量
【发布时间】:2011-09-05 11:17:37
【问题描述】:

我的应用名称是 PropertiesSearch,因此该项目包含以下文件:

PropertiesSearchAppDelegate.h
PropertiesSearchAppDelegate.m
PropertiesSearchViewController.h
PropertiesSearchViewController.m

我在 PropertiesSearchAppDelegate.h 中声明了一个实例变量 (ListingNav),如下所示:

#import <UIKit/UIKit.h>

@class PropertiesSearchViewController;

@interface PropertiesSearchAppDelegate : NSObject <UIApplicationDelegate> {
    UINavigationController *ListingNav;
}

@property (nonatomic, retain) UINavigationController *ListingNav;

@end

在 PropertiesSearchAppDelegate.m 中,我添加了

@synthesize ListingNav; 

现在我正在尝试从 PropertiesSearchViewController.m 访问 ListingNav,如下所示:

PropertiesSearchAppDelegate *appDelegate = (PropertiesSearchAppDelegate *)[[UIApplication sharedApplication] delegate];
// Then here I can easily do appDelegate.ListingNav etc...

但调试器正在显示:

使用未声明的标识符PropertiesSearchAppDelegate

我应该在 PropertiesSearchViewController.h 中导入 PropertiesSearchAppDelegate.h 吗?

提前感谢您的帮助

斯蒂芬

【问题讨论】:

    标签: iphone ios ios4 uiapplicationdelegate uiapplication


    【解决方案1】:

    您必须在 PropertiesSearchViewController.h 中导入 PropertiesSearchAppDelegate.h

    要让一个类知道另一个类,您应该导入该类 或添加 @class 指令。添加 @class 指令 只是让类知道存在另一个类。在您的情况下,您应该在尝试访问该类的成员时导入 PropertiesSearchAppDelegate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多