Iphone Objective-C中的控制流

Iphone Objective-C中的控制流,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我正在努力完成。书中有这样一种方法: -(void)playWinSound { NSString *path = [[NSBundle mainBundle] pathForResource:@"win" ofType:@"wav"]; SystemSoundID soundID; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID); Aud

我正在努力完成。书中有这样一种方法:

-(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 = @"";
}
-(无效)播放声音
{
NSString*path=[[NSBundle mainBundle]pathForResource:@“win”类型:@“wav”];
系统声音ID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound(soundID);
winLabel.text=@“赢!”;
[自执行选择器:@selector(showButton),对象:nil afterDelay:1.5];
}
-(i动作)旋转{
布尔温=否;
int numInRow=1;
int lastVal=-1;
对于(int i=0;i<5;i++)
{
int newValue=random()%[self.column1 count];
if(newValue==lastVal)
numInRow++;
其他的
numInRow=1;
lastVal=新值;
[picker selectRow:newValue不完整项:i动画:是];
[选择器重新加载组件:i];
如果(numInRow>=3)
赢=是;
}
button.hidden=是;
NSString*path=[[NSBundle mainBundle]pathForResource:@“crunch”类型:@“wav”];
系统声音ID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound(soundID);
如果(赢)
[自执行选择器:@selector(playWinSound),对象:nil afterDelay:.5];
其他的
[自执行选择器:@selector(showButton),对象:nil afterDelay:.5];
winLabel.text=@;
}

单击“旋转”按钮时,它将调用此旋转方法。如果win为YES,则调用playWinSound,将winLabel的值更改为@“win!”。为什么如果旋转导致赢,winLabel中的文本会变为@“赢!”并保持这种状态。流不应该返回到将winLabel更改为@“的spin方法吗?

我认为发生的事情是,通过调用PerformSelect方法,它得到了后延迟期…因此方法排队,执行winLabel.text=@”代码,然后启动playWinSound方法,再次更改标签。

我认为,通过调用performSelector方法,它得到了后延迟时间……因此该方法排队,执行winLabel.text=@“”代码,然后启动playWinSound方法,再次更改标签。

是的,流确实返回到spin方法。诀窍在于执行
playWinSound
方法的调用:

[self performSelector:@selector(playWinSound) withObject:nil afterDelay:.5];
注意该方法的后延迟部分。这将安排在0.5秒后的第一个可用时间调用
playWinSound
。具体来说,调用将在0.5秒后的第一次运行循环开始时进行。此方法正在已运行的运行循环中调用,因此在
spin
方法返回之前,
playWinSound
不可能执行

这就是说,这似乎是一种非常奇怪的方案结构。我假设他们正在将
winLabel.text
设置为
@”“
,以确保它被重置为空字符串,除非它特别变为
@“Win!”
,但我的结构会非常不同。然而,这就是它工作的原因。

是的,流确实返回到spin方法。诀窍在于执行
playWinSound
方法的调用:

[self performSelector:@selector(playWinSound) withObject:nil afterDelay:.5];
[self performSelector:@selector(playWinSound) withObject:nil afterDelay:.5];
注意该方法的后延迟部分。这将安排在0.5秒后的第一个可用时间调用
playWinSound
。具体来说,调用将在0.5秒后的第一次运行循环开始时进行。此方法正在已运行的运行循环中调用,因此在
spin
方法返回之前,
playWinSound
不可能执行

这就是说,这似乎是一种非常奇怪的方案结构。我假设他们正在将
winLabel.text
设置为
@”“
,以确保它被重置为空字符串,除非它特别变为
@“Win!”
,但我的结构会非常不同。然而,这就是为什么它有效

[self performSelector:@selector(playWinSound) withObject:nil afterDelay:.5];
此方法对操作进行排队,立即返回并将文本重置为“”。如果它实际等待,然后在超时后调用选择器,则会浪费资源

然后在超时后执行该操作,并将文本设置为“赢”

:

此方法设置一个计时器来执行 当前服务器上的aSelector消息 线程的运行循环

此方法对操作进行排队,立即返回并将文本重置为“”。如果它实际等待,然后在超时后调用选择器,则会浪费资源

然后在超时后执行该操作,并将文本设置为“赢”

:

此方法设置一个计时器来执行 当前服务器上的aSelector消息 线程的运行循环