0

是否有可能从 for...loop 中跳出来?在 ViewController 我有一个循环。在那个循环中,我有一些系统声音一个接一个地播放。for..loop 从 plist 向上计数。它还有一个睡眠功能来获得一些延迟。我用一个按钮开始循环,并希望有一个停止按钮。但只要循环进行,它就不会响应任何按钮的另一次按下。想知道是否有可能停止这个循环以及如何停止?

4

2 回答 2

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.

于 2013-03-28T10:03:02.617 回答
1

看下面这个例子

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...
     }
}
于 2013-03-28T09:58:12.397 回答