プロジェクトにアプリデリゲートと3つのビューコントローラーがあります。App Delegateに変数(NSMutable配列)があり、ViewControllerからアクセスしたいと思います。そこで、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
そして最後に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];
で、コンパイラは初期化要素が一定ではないと言います。
私は答えを求めてウェブを調べましたが、すべてうまくいくようです...しかし、私にはサイコロはありません:(どこが間違っているのか教えていただけますか?私はXCode3.2とiOSSDK3を使用しています...おそらくSDKが問題です。
ありがとうございました