objective-c - 设置 App Delegate 以管理数据

标签 objective-c ios appdelegate

我正在开发一个带有 Split View Controller 的应用程序,我想将主要数据类存储在 App Delegate 中,以便我可以从多个 View (MasterView、DetailView 和几个 PopUps)访问它。

我有点菜鸟,不知道为什么我会收到错误:

AppDelegate.m:31:26: Property 'dataController' not found on object of type 'MasterViewController'

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

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

最佳答案

你需要添加

#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;

就像对编译器说:

Yo... compiler. I'm going to be talking about something called MasterViewController in this header file and you don't know what a MasterViewController is... but don't trip out bro. It's all good.

但是,稍后在您编写代码后的 .m 文件中:

... masterViewController.dataController ...

编译器响应:

Whoa, whoa, whoa. I was cool with you just having a variable with the type MasterViewController in the header file because you told me it was all good and not to worry. But now here you are trying to use some aspect of MasterViewController that I know nothing about. How am I supposed to know if this is legal or not? Not cool bro.

关于objective-c - 设置 App Delegate 以管理数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13549230/

相关文章:

iphone - Sqlite 选择列并删除重复项

ios - iOS 版 UE4 AR(Kit) 开发中出现图标错误

ios - Xcode iOS 模拟器中的远程 Web 检查器

iOS - 从 ViewController 调用 App Delegate 方法

ios - NSHTTPCookieStorage 不会自动保存 cookie

iphone - 当手绕传感器移动时,接近传感器打开

ios - 当我从 Scheme Url 启动应用程序时未调用 openUrl

ios - 无法使用 Storyboard 设置将出现在 App Startup 上的第一个 View Controller

objective-c - 计算n次方根?

ios - 无法更改共享扩展导航栏标题颜色(Swift)