如何在切换视图时打破for循环?

我需要帮助,我正在写一个简单的比赛游戏。我的问题是,当我从游戏视图切换到主菜单视图时,游戏一直播放,直到循环在背景中运行,点亮按钮并按顺序点亮它们,但仍然可以听到按钮时发出的声音当我切换视图时在后台播放灯光。当我按下“主菜单”按钮时,我试图在循环中添加一个中断,但它不起作用,我标记了按钮'100'。下面是我写的相关代码,请记住,我仍然是一个新手,所以任何建议将不胜感激!先谢谢你!如何在切换视图时打破for循环

- (IBAction)newGame:(id)sender 

{

//creat random array of buttons

[self randomArray];

//initialize button pushed array

buttonPushedArray = [[NSMutableArray alloc] initWithArray:NULL];

//initialize points

points = [[NSNumber alloc] initWithInt: 30];

//Initialize label to show score

scoreView.text = [NSString stringWithFormat:@"%@", score];

//Initialize label to show level

levelView.text = [NSString stringWithFormat:@"%@", levelNum];

//button pushed

//int a = [sender tag];

//loop through each button in array and turn them on and off

for (NSString *i in gameArray)

{

if ([sender tag] > 99)

{

break;

}else

{

int butNum = [i intValue];

delay2 = [delay intValue];

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

//turn button on

[self buttonLit:(NSNumber*)number];

//turn button off

[self performSelector:@selector(buttonUnLit:) withObject:number afterDelay:delay2];

//time delay that give 'turn button off' command enough time to respond

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow: delay2]];

[number release];

}

}

}

- (void)randomArray

{

//initialize array

gameArray = [[NSMutableArray alloc] initWithArray:NULL];

int level = [levelNum intValue];

//************* 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:(NSNumber*)number

{

int numLitInt = [number intValue];

if (numLitInt == 0)

{

//light up the button

[button0 setImage:[UIImage imageNamed:@"0(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding1.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 1)

{

//light up the button

[button1 setImage:[UIImage imageNamed:@"1(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding2.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 2)

{

//light up the button

[button2 setImage:[UIImage imageNamed:@"2(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding3.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 3)

{

//light up the button

[button3 setImage:[UIImage imageNamed:@"3(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding4.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 4)

{

//light up the button

[button4 setImage:[UIImage imageNamed:@"4(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding1.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 5)

{

//light up the button

[button5 setImage:[UIImage imageNamed:@"5(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding2.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 6)

{

//light up the button

[button6 setImage:[UIImage imageNamed:@"6(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding3.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 7)

{

//light up the button

[button7 setImage:[UIImage imageNamed:@"7(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding4.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 8)

{

//light up the button

[button8 setImage:[UIImage imageNamed:@"8(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding1.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 9)

{

//light up the button

[button9 setImage:[UIImage imageNamed:@"9(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding2.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 10)

{

//light up the button

[button10 setImage:[UIImage imageNamed:@"10(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding3.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}else if (numLitInt == 11)

{

//light up the button

[button11 setImage:[UIImage imageNamed:@"11(lite).png"] forState:UIControlStateNormal];

//*********** play audio file ****************

NSString* resourcePath = [[NSBundle mainBundle] resourcePath];

resourcePath = [resourcePath stringByAppendingString:@"/ding4.m4a"];

NSError* err;

//Initialize our player pointing to the path to our resource

player = [[AVAudioPlayer alloc] initWithContentsOfURL:

[NSURL fileURLWithPath:resourcePath] error:&err];

if(err){

//bail!

NSLog(@"Failed with reason: %@", [err localizedDescription]);

}

else{

//set our delegate and begin playback

[player play];

}

return;

}

}

- (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;

}

}

- (void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:animated];

[player stop];

}

回答:

您可以添加另一个var来确定线程是否应该中断。 像:

您的.h文件中

Bool flag; 

您的.m文件

-(void)yourBackgroundMethod{ 

if(flag)

break;

}

以上是 如何在切换视图时打破for循环? 的全部内容, 来源链接: utcz.com/qa/266786.html

回到顶部