私はBeginning iPhone Developmentで作業しています。本書では、この方法はObjective-Cの制御フロー
-(void)playWinSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"win" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
winLabel.text = @"WIN!";
[self performSelector:@selector(showButton) withObject:nil afterDelay:1.5];
}
-(IBAction)spin{
BOOL win = NO;
int numInRow = 1;
int lastVal = -1;
for (int i = 0; i < 5; i++)
{
int newValue = random() % [self.column1 count];
if (newValue == lastVal)
numInRow++;
else
numInRow = 1;
lastVal = newValue;
[picker selectRow:newValue inComponent:i animated:YES];
[picker reloadComponent:i];
if (numInRow >= 3)
win = YES;
}
button.hidden = YES;
NSString *path = [[NSBundle mainBundle] pathForResource:@"crunch" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound (soundID);
if (win)
[self performSelector:@selector(playWinSound) withObject:nil afterDelay:.5];
else
[self performSelector:@selector(showButton) withObject:nil afterDelay:.5];
winLabel.text = @"";
}
です。スピンボタンをクリックすると、このスピンメソッドが呼び出されます。勝利がYESの場合、winLabelの値を@ "Win!"に変更するplayWinSoundが呼び出されます。スピンの結果が勝つと、winLabelのテキストが@ "Win!"に変わるのはなぜですか?そのようにとどまる。フローは、winLabelを@ ""に変更するスピンメソッドに戻るべきではありませんか?