0

Is it possible to break out from a for...loop? In a ViewController I have a loop. Inside that loop I have some system sounds playing one after each other. The for..loop counts upwards from a plist. It has also a sleep function to get some delay. I start the loop with a button and would like to have a stop button. But as long as the loop is going it does not respond to another press at any button. Wonder if it is possible to stop this loop and how?

Lars -
  • 501
  • 1
  • 10
  • 27

2 Answers2

4

But as long as the loop is going it does not respond to another press at any button.

Well you have a slightly different problem here. What you're doing is blocking the UI thread with your loop. Breaking out of the loop won't have much effect, because you won't be able to use the UI while the loop is "looping".

You should probably look into threading - if you play your music on a new thread you leave the UI thread still open and able to receive touch input.

You can always do what Paras Joshi has said in his answer in that thread, or you could use an NSTimer to create a loop. You can invalidate a timer to stop it from calling.

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
1

see this bellow example

for(int i = 0;i<[yourFirstArray count];i++){
    for(int j = 0;j<[yourSecondArray count];j++){ 
           if (yourConition)
                  break; // here you break out from forloop if "if" condition is true...
     }
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Yes that is nice but since "myCondition" here is a tap at a button and the UI is blocked I don´t understand how I can get it to break – Lars - Mar 28 '13 at 10:22
  • here i also think about that , here you can set some condition with NSTimer .. i used this type of logic when i store current location in every 2 seconds, here call that method which store this for loop from your some time otherwise use NSThread , see this link hope you got some Idea from this link http://stackoverflow.com/questions/1926879/objective-c-how-to-kill-a-thread-while-in-another-thread-in-objective-c/1926920#1926920 – Paras Joshi Mar 28 '13 at 10:26
  • @Lars- see this link for Thread Tutorial... https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html – Paras Joshi Mar 28 '13 at 10:29