I’m working on an app with a Split View controller and would like to store the main data class in the App Delegate so I can access it from multiple views (MasterView, DetailView, and several PopUps).
I’m a bit of a noob and can’t figure out why Im getting the error:
AppDelegate.m:31:26: Property ‘dataController’ not found on object of type ‘MasterViewController’
Below is the relevant code – any help is much appreciated. Thanks.
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
You need to add
to AppDelegate.m.
Explantation:
From your error message I can tell that you’re trying to access a property of MasterViewController on line 26 of your AppDelegate. However, in your AppDelegate class you only have a forward declaration (“@class MasterViewController”) and you don’t include the actual header file for MasterViewController. What this means is that AppDelegate knows there exists a class named MasterViewController somewhere in your project… but that’s all it knows. AppDelegate doesn’t know anything about the contents of MasterViewController, i.e. properties or methods MasterViewController declares.
The reason to use @class in a header file is so that you can have properties in an @interface such as
without having to import MasterViewController.h in AppDelegate.h. This way, other files in your project can import AppDelegate.h without also inadvertently importing MasterViewController.h.
Translation:
You writing the code:
is like saying to the compiler:
But then, later in the .m file after you write the code:
the compiler responds: