引用 AppDelegate 时出错
我的项目中有一个应用程序委托和 3 个视图控制器。我的应用程序委托中有一个变量(NSMutable 数组),我想从视图控制器访问它。因此,我决定创建一个指向我的应用程序委托的指针并访问变量。 这是我的代码:
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
最后是referenceViewController.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的问题。
也许
I have an App Delegate and a 3 view controllers in my project. I have a variable(a NSMutable Array) in my App Delegate which I want to access from my view controllers. So I decided to create a pointer to my App Delegate and access the variables.
Here is my code:
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
The view controller class from which I want to access the 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
And finally referenceViewController.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
So anyway at the line data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate];
the compiler says that the initializer element is not constant.
I have scrouged the web for answers, and for all it seems to work...but no dice for me :( Can you please advice me on where I have gone wrong? I am using XCode 3.2 and iOS SDK 3....maybe the SDK is the problem.
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该行代码不在方法或函数中,因此编译器将其视为编译时常量或静态/全局变量的定义。这些需要常量值进行初始化。
您应该将
data
的赋值放在方法中。一个好地方是-viewDidLoad
:That line of code isn't in a method or function, so the compiler is treating it as the definition of a compile-time constant or static/global variable. Those need constant values for initialization.
You should put the assignment of
data
within a method. A good place would be-viewDidLoad
:我弄清楚了结构和联合的问题。我所要做的就是将我的referenceViewController.h 文件中的
@class iSolveAppDelegate
更改为#import "iSolveAppDelegate.h"
。感谢乔纳森的帮助!I figured out the struct and union problem. All I had to do was change
@class iSolveAppDelegate
to#import "iSolveAppDelegate.h"
in my referenceViewController.h file. Thanks Jonathan for your help!