以下是我的代码(省略了一些无关的东西):
@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。
那么,我的问题在哪里?