设置App Delegate以管理数据

时间:2012-11-25 07:44:39

标签: objective-c ios appdelegate

我正在开发一个带有Split View控制器的应用程序,并希望将主数据类存储在App Delegate中,以便我可以从多个视图(MasterView,DetailView和几个PopUps)访问它。

我有点像菜鸟,无法弄清楚为什么我会收到错误:

  

AppDelegate.m:31:26:在'MasterViewController'类型的对象上找不到属性'dataController'

以下是相关代码 - 非常感谢任何帮助。感谢。

AppDelegate.h

#import <UIKit/UIKit.h>

@class EventClassDataController;
@class MasterViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

#import <UIKit/UIKit.h>

@class EventClassDataController;
@class MasterViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

MasterViewController.h

#import <UIKit/UIKit.h>

@class DetailViewController;
@class EventClassDataController;

@interface MasterViewController : UITableViewController

@property (strong, nonatomic) EventClassDataController *dataController;
@property (strong, nonatomic) DetailViewController *detailViewController;

@end

MasterViewController.m

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "EventClassDataController.h"
#import "EventClass.h"

@interface MasterViewController ()

@end

@implementation MasterViewController

@synthesize detailViewController, dataController;

- (void)awakeFromNib
{
    self.clearsSelectionOnViewWillAppear = NO;
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
    [super awakeFromNib];

    // Initialize event data
    self.dataController = [[EventClassDataController alloc] init];
}

EventClassDataController.h

#import <Foundation/Foundation.h>

@class EventClass;

@interface EventClassDataController : NSObject

@property (nonatomic, copy) NSMutableArray *masterEventList;

-(NSUInteger)countOfList;
-(EventClass *)objectInListAtIndex:(NSUInteger)theIndex;
-(void)addNewEvent:(EventClass *)event;
-(void)removeEvent:(EventClass *)event;

@end

1 个答案:

答案 0 :(得分:0)

您需要添加

#import "MasterViewController.h"

到AppDelegate.m。

<强>外植

从您的错误消息我可以告诉您,您正在尝试访问AppDelegate第26行的MasterViewController属性。但是,在您的AppDelegate类中,您只有一个前向声明(“@class MasterViewController”),并且您不包含MasterViewController的实际头文件。这意味着AppDelegate知道项目中某处存在一个名为MasterViewController的类......但这就是它所知道的。 AppDelegate对MasterViewController的内容一无所知,即MasterViewController声明的属性或方法。

在头文件中使用@class的原因是你可以在@interface中拥有属性,例如

@property (nonatomic, strong) MasterViewController * appDelegatesReferenceToMasterViewController;

无需在AppDelegate.h中导入MasterViewController.h。这样,项目中的其他文件可以导入AppDelegate.h而不会无意中导入MasterViewController.h。

<强>翻译

您编写代码:

@class MasterViewController;

就像对编译器说:

  哟...编译器。我将在这个头文件中谈论一个名为MasterViewController的东西,你不知道MasterViewController是什么......但是不要跳出兄弟。这一切都很好。

然后,在编写代码之后,在.m文件中:

... masterViewController.dataController ...

编译器响应:

  

哇,哇,哇。我很酷你只是在头文件中有一个MasterViewController类型的变量因为你告诉我这一切都很好而且不用担心。但是现在你在尝试使用MasterViewController的一些方面,我一无所知。我怎么知道这是否合法?不酷的兄弟。