gpt4 book ai didi

iphone - 具有时间延迟的 performSelector 在 for 循环中不起作用 - iPhone

转载 作者:行者123 更新时间:2023-11-29 13:42:05 26 4
gpt4 key购买 nike

我希望有人能帮助我。我正在编写一个简单的游戏,当用户按下一个按钮时,会生成一个与按钮编号相对应的数字数组,然后使用 for 循环按时间延迟依次打开和关闭每个按钮。问题是阵列中的所有按钮都打开,只有最后一个关闭。就像 perfomSelector with time delay 命令只在 for 循环结束时执行,有人知道为什么吗?

这是我使用的代码:

- (IBAction)buttonPushed:(id)sender;
{
//creat random array of buttons
[self randomArray];

//loop through each button in array and turn them on and off
for (NSString *i in gameArray)
{
int butNum = [i intValue];

numLit = [[NSNumber alloc] initWithInt:butNum];

//turn button on
[self buttonLit];

//turn button off
[self performSelector:@selector(buttonUnLit) withObject:nil afterDelay:1.0];

}

}

- (void)randomArray
{
//initialize array
gameArray = [[NSMutableArray alloc] initWithArray:NULL];

//************* Build random array of numbers *******************
for (int i = 0; i < level; i++)
{
//generate a number from 0 to 11 at random
NSInteger num = (arc4random() % 11);

//add number to array
[gameArray addObject:[NSString stringWithFormat:@"%i", num]];
}
return;

}

- (void)buttonLit
{
int numLitInt = [numLit intValue];


if (numLitInt == 0)
{
[button0 setImage:[UIImage imageNamed:@"0(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 1)
{
[button1 setImage:[UIImage imageNamed:@"1(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 2)
{
[button2 setImage:[UIImage imageNamed:@"2(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 3)
{
[button3 setImage:[UIImage imageNamed:@"3(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 4)
{
[button4 setImage:[UIImage imageNamed:@"4(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 5)
{
[button5 setImage:[UIImage imageNamed:@"5(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 6)
{
[button6 setImage:[UIImage imageNamed:@"6(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 7)
{
[button7 setImage:[UIImage imageNamed:@"7(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 8)
{
[button8 setImage:[UIImage imageNamed:@"8(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 9)
{
[button9 setImage:[UIImage imageNamed:@"9(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 10)
{
[button10 setImage:[UIImage imageNamed:@"10(lite).png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 11)
{
[button11 setImage:[UIImage imageNamed:@"11(lite).png"] forState:UIControlStateNormal];
return;
}

}


- (void)buttonUnLit
{
int numLitInt = [numLit intValue];


if (numLitInt == 0)
{
[button0 setImage:[UIImage imageNamed:@"0.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 1)
{
[button1 setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 2)
{
[button2 setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 3)
{
[button3 setImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 4)
{
[button4 setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 5)
{
[button5 setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 6)
{
[button6 setImage:[UIImage imageNamed:@"6.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 7)
{
[button7 setImage:[UIImage imageNamed:@"7.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 8)
{
[button8 setImage:[UIImage imageNamed:@"8.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 9)
{
[button9 setImage:[UIImage imageNamed:@"9.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 10)
{
[button10 setImage:[UIImage imageNamed:@"10.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 11)
{
[button11 setImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
return;
}

}

最佳答案

你的问题很明显,你正在通过 for 循环更改 numLit,你希望选择器在一秒钟内执行,当第一个选择器触发时你已经将 numLit 更改为最后一个数字,这就是为什么所有火灾修改最后一个按钮......你应该通过将数字传递给buttonUnlit来解决这个问题,这样你就不会依赖于你不断改变的类变量......就像

- (void)buttonUnLit:(NSNumber*)number
{
int numLitInt = [number intValue];


if (numLitInt == 0)
{
[button0 setImage:[UIImage imageNamed:@"0.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 1)
{
[button1 setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 2)
{
[button2 setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 3)
{
[button3 setImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 4)
{
[button4 setImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 5)
{
[button5 setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 6)
{
[button6 setImage:[UIImage imageNamed:@"6.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 7)
{
[button7 setImage:[UIImage imageNamed:@"7.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 8)
{
[button8 setImage:[UIImage imageNamed:@"8.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 9)
{
[button9 setImage:[UIImage imageNamed:@"9.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 10)
{
[button10 setImage:[UIImage imageNamed:@"10.png"] forState:UIControlStateNormal];
return;
}else if (numLitInt == 11)
{
[button11 setImage:[UIImage imageNamed:@"11.png"] forState:UIControlStateNormal];
return;
}

}

- (IBAction)buttonPushed:(id)sender;
{
//creat random array of buttons
[self randomArray];
float dalay=1.0f;
//loop through each button in array and turn them on and off
for (NSString *i in gameArray)
{
int butNum = [i intValue];

NSNumber *num = [[NSNumber alloc] initWithInt:butNum];

//turn button on
[self buttonLit];

//turn button off
[self performSelector:@selector(buttonUnLit) withObject:num afterDelay:delay];
delay+=0.5f;
[num release];

}

}

关于iphone - 具有时间延迟的 performSelector 在 for 循环中不起作用 - iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8810201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com