如何暂停循环以请求输入 (iPhone)
我有一个相当简单的循环操作,用于检查数据结构中的明显错误和可能的问题。我不会详细介绍它。我想要做的是每当遇到错误时暂停此循环的执行,以便我可以在继续检查剩余数据之前询问用户他们想对该错误执行什么操作。
谁能给出关于如何最好地做到这一点的任何想法?
-灰
I have a fairly simple looped operation that checks for obvious errors and likely problems in a data structure. I won't go into detail about it. What I want to be able to do is pause the execution of this loop whenever an error is encountered so that I can ask the user what they want to do about that error, before continuing to check the remaining data.
Can anyone give any ideas about how best to do that?
-Ash
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这将使循环停止 0.25 秒,但这不是您想要做的。您需要首先重新表述您的问题。
This will stop your loop for 0.25 seconds, but it's not what you are looking to do. You need to reformulate your question first.
不要在 UI 运行循环中的循环内执行长时间操作。它向用户呈现无响应的 UI;如果应用程序锁定时间过长,操作系统可能会杀死该应用程序。
将循环分解为短回调(使每个循环迭代的内部成为一个方法),并在大约几分之一秒的内部循环操作后退出每个回调。
或者将循环作为后台线程中的任务执行,并使用锁来停止循环,同时等待来自前台 UI 运行循环的有关对某些循环状态执行哪些操作的消息。
Don't do long operations inside a loop in the UI run loop. It presents a non-responsive UI to the user; and the OS may kill the app if it locks up for too long.
Break the loop into short callbacks (make the inside of each loop iteration a method), and exit each callback after maybe a fraction of a seconds worth of inner loop operations.
Or execute the loop as a task in a background thread, and use locks to stop the loop while waiting for messages from the foreground UI run loop about what to do for some loop state.
似乎 UIAlertView 就是您正在寻找的:
UIAlertView类参考
Seems like UIAlertView is what you are looking for:
UIAlertView Class Reference
UIAlertView 是异步的,因此它不会暂停循环。
有更聪明的方法来编写循环,例如使用块或完成处理程序,但我建议最简单的方法是将代码从:转换
为:
...并在 UIAlertView 的回调方法中,执行:
...ie一次运行一次的循环,让您任意停止/暂停/恢复它。
UIAlertView is asynch, so it won't pause the loop.
There are cleverer ways to write your loop, e.g. using blocks or completion handlers, but I'd suggest the EASIEST way is simply convert your code from:
to:
...and in the callback method from UIAlertView, do:
...i.e. a loop that runs once at a time, and lets you arbitrarily stop / pause / resume it.