0

プロジェクトにアプリデリゲートと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が問題です。

ありがとうございました

4

2 に答える 2

2

そのコード行はメソッドまたは関数に含まれていないため、コンパイラはそれをコンパイル時定数または静的/グローバル変数の定義として扱います。それらは初期化のために定数値を必要とします。

の割り当てはdataメソッド内に配置する必要があります。良い場所は次のようになります-viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];

    data = (iSolveMathAppDelegate *)[[UIApplication sharedApplication] delegate]; 

    ...
}
于 2011-03-23T00:38:58.013 に答える
0

構造体と結合の問題を理解しました。私がしなければならなかったのは、referenceViewController.hファイルのに変更@class iSolveAppDelegateすることだけでした。#import "iSolveAppDelegate.h"ジョナサン、助けてくれてありがとう!

于 2011-03-23T03:07:37.787 に答える