ねえ、私はObjective-C 2.0とXcodeを初めて使用するので、ここで初歩的なものが欠けている場合はご容赦ください。とにかく、私は新しいビューを表示するためにGameViewと呼ばれる独自のUIViewControllerクラスを作成しようとしています。ゲームを動作させるには、plistファイルからロードするNSArrayを追跡する必要があります。正しいNSArrayをインスタンス変数にロードしたいメソッド「loadGame」を作成しました。ただし、メソッドの実行後、インスタンス変数は配列を追跡できなくなるようです。コードを表示するだけで簡単になります。
@interface GameView : UIViewController {
IBOutlet UIView *view
IBOutlet UILabel *label;
NSArray *currentGame;
}
-(IBOutlet)next;
-(void)loadDefault;
...
@implementation GameView
- (IBOutlet)next{
int numElements = [currentGame count];
int r = rand() % numElements;
NSString *myString = [currentGame objectAtIndex:(NSUInteger)r];
[label setText: myString];
}
- (void)loadDefault {
NSDictionary *games;
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Games.plist"];
games = [NSDictionary dictionaryWithContentsOfFile:finalPath];
currentGame = [games objectForKey:@"Default"];
}
loadDefaultが呼び出されると、すべてが完全に実行されますが、後でnextへのメソッド呼び出しでcurrentGame NSArrayを使用しようとすると、currentGameはnilのように見えます。このコードのメモリ管理の問題も認識しています。この問題については、どんな助けでもいただければ幸いです。