【发布时间】:2011-03-23 00:33:59
【问题描述】:
我的项目中有一个 App Delegate 和 3 个视图控制器。我的 App Delegate 中有一个变量(一个 NSMutable 数组),我想从我的视图控制器访问它。所以我决定创建一个指向我的 App Delegate 的指针并访问变量。 这是我的代码:
iSolveMathAppDelegate.h
#import <UIKit/UIKit.h>
@interface iSolveMathAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
NSMutableArray *tmpArray;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) NSMutableArray *tmpArray; // variable I want to access
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end
iSolveMathAppDelegate.m
#import "iSolveMathAppDelegate.h"
@implementation iSolveMathAppDelegate
@synthesize window;
@synthesize tabBarController;
@synthesize tmpArray;
...
- (void)dealloc {
[tabBarController release];
[window release];
[tmpArray release];
[super dealloc];
}
@end
我要从中访问 tmpArray 的视图控制器类。
referenceViewController.h
#import <UIKit/UIKit.h>
@class iSolveMathAppDelegate;
@interface referenceViewController : UITableViewController {
NSMutableArray *equationTypes;
iSolveMathAppDelegate *data;
}
@property(nonatomic, retain) NSMutableArray *equationTypes;
@property(nonatomic, retain) iSolveMathAppDelegate *data;
@end
最后参考ViewController.m
#import "referenceViewController.h"
@implementation referenceViewController
@synthesize equationTypes, data;
data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate];
//says that initializer element is not constant...ERROR!
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"equationTemplates"ofType:@"plist"];
data.tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.equationTypes = data.tmpArray;
[data.tmpArray release]; // obviously none of these work, as data is not set.
}
- (void)dealloc {
[super dealloc];
[equationTypes release];
[data release];
}
@end
所以无论如何,在data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; 行,编译器说初始化元素不是常量。
我已经在网上寻找答案,而且它似乎工作......但对我来说没有骰子:(你能告诉我我哪里出错了吗?我正在使用 XCode 3.2 和 iOS SDK 3。 ...也许是 SDK 的问题。
谢谢
【问题讨论】:
标签: iphone xcode elements viewcontroller uiapplicationdelegate