如何暂停循环以请求输入 (iPhone)

发布于 2024-10-22 06:32:34 字数 150 浏览 8 评论 0原文

我有一个相当简单的循环操作,用于检查数据结构中的明显错误和可能的问题。我不会详细介绍它。我想要做的是每当遇到错误时暂停此循环的执行,以便我可以在继续检查剩余数据之前询问用户他们想对该错误执行什么操作。

谁能给出关于如何最好地做到这一点的任何想法?

-灰

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

奶气 2024-10-29 06:32:34

这将使循环停止 0.25 秒,但这不是您想要做的。您需要首先重新表述您的问题。

CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.25, false);

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.

CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.25, false);
暮色兮凉城 2024-10-29 06:32:34

不要在 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.

青瓷清茶倾城歌 2024-10-29 06:32:34

似乎 UIAlertView 就是您正在寻找的:

UIAlertView类参考

Seems like UIAlertView is what you are looking for:

UIAlertView Class Reference

我要还你自由 2024-10-29 06:32:34

UIAlertView 是异步的,因此它不会暂停循环。

有更聪明的方法来编写循环,例如使用块或完成处理程序,但我建议最简单的方法是将代码从:转换

-(void) method
{
...
for( int i=0; i<10; i++)
{
check_loop_item(i);
}
}

为:

int iCurrent, iEnd;

-(void) method
{
...
iCurrent = 0;
iEnd = 10;
[self doLoop];
}

-(void) doLoop
{
if( iCurrent >= iEnd )
return;

check_loop_item(iCurrent);
...
if( error )
{
// Popup a UIAlertView
}
else
{
iCurrent++;
[self doLoop];
}

...并在 UIAlertView 的回调方法中,执行:

-(void) callbackFromUIAlertView
{
iCurrent++;
[self doLoop];
}

...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:

-(void) method
{
...
for( int i=0; i<10; i++)
{
check_loop_item(i);
}
}

to:

int iCurrent, iEnd;

-(void) method
{
...
iCurrent = 0;
iEnd = 10;
[self doLoop];
}

-(void) doLoop
{
if( iCurrent >= iEnd )
return;

check_loop_item(iCurrent);
...
if( error )
{
// Popup a UIAlertView
}
else
{
iCurrent++;
[self doLoop];
}

...and in the callback method from UIAlertView, do:

-(void) callbackFromUIAlertView
{
iCurrent++;
[self doLoop];
}

...i.e. a loop that runs once at a time, and lets you arbitrarily stop / pause / resume it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文