以下は私のコードです(いくつかの無関係なものは省略されています):
@implementation HomeSceneController
...
@synthesize options = _options; // _options is a NSArray object with 4 elements
- (id)init
{
if (self = [super initWithNibName:@"HomeScene" bundle:nil]) {
_currentOptionIndex = 0;
// Following code add two key event observations, when up arrow or down arrow key is pressed, the corresponding function will be fired.
[self addObservation:_KEY_UPARROW_ selector:@selector(UpArrowPressHandler)];
[self addObservation:_KEY_DOWNARROW_ selector:@selector(DownArrowPressHandler)];
}
return self;
}
- (void)loadView {
[super loadView];
// init _options
_options = [NSArray arrayWithObjects:
_localGameOption,
_networkGameOption,
_controlSettingOption,
_quitOption,
nil];
[self selectOption:_localGameOption];
}
....
// in these two functions, _options become nil! I don't know why...
- (void)UpArrowPressHandler {
if (_currentOptionIndex > 0) {
[self deselectOption:_options[_currentOptionIndex]];
_currentOptionIndex--;
[self selectOption:_options[_currentOptionIndex]];
}
}
- (void)DownArrowPressHandler {
if (_currentOptionIndex < 3) {
[self deselectOption:_options[_currentOptionIndex]];
_currentOptionIndex++;
[self selectOption:_options[_currentOptionIndex]];
}
}
@end
上矢印キーを押すと、UpArrowPressHandler関数が起動します。ただし、問題は、_options配列がnilになることです。
誰かがそれを修正する理由と方法を教えてもらえますか?
//===========================================================================================
追加の問題:
次のプログラムでは:
import "Deep.h"
@implementation Deep
- (id)init {
if (self = [super init]) {
_name = @"Deep";
}
return self;
}
- (void)test {
NSLog(_name);
}
@end
テストメソッドは、別の場所で呼び出すと「Deep」を正しく出力できます。
ただし、@ ATaylorの説明によると、_nameはリリースする必要があります。
それで、私の問題はどこにありますか?