0

很抱歉没有发布任何代码,因为我不知道这个逻辑。

我有一个while循环,只要用户按下按钮,它就会运行并在他再次按下后停止。但是我有一个条件需要检查一小段时间,直到它的条件得到满足(定时器不能满足我的需要)。所以我想执行那部分代码,直到它满足该条件,然后我不需要那部分代码来检查该条件,或者换句话说,我希望程序停止执行那部分代码以便我可以保存我为那部分代码浪费了执行时间。

那么这可能吗?或者这是我们在编程中无法实现的?

请注意,我不想实现计时器来完成这项工作!

已编辑(我需要建立的工作)

我正在检查的是加速度计更新...当用户按下按钮时更新开始。我已经设置了一个条件来检查它是否超过了某个值,如果超过了我想将一个计数存储到一个全局变量中。然后就像我说的那样,我不需要它来重复检查那个条件。因为我一旦开始就不会停止加速数据,除非用户按下按钮停止?

4

2 回答 2

2

I have no idea what your code will look like... but yes, this is something you can easily accomplish via Notifications (i.e. register for "NSNotifications" and when some condition is satisfied, it posts a notification and then any other objects listening will run a selector to "stop"). Or you can have your other threads monitoring some condition via Key-Value-Observing, so when some BOOL condition (that each of your sections of code are observing) changes, they can finish up whatever they're doing.

Hopefully this makes sense so far, but the overall idea is "yes, what you're asking for shouldn't be too hard to do".

于 2012-06-23T11:34:27.140 回答
0
while(mainCondition)
{
    if(!extraCondidion)
    {
        // Do extra bit of code only until extra condition is met
    }

    // Do normal code that runs before and after extra condition is met
}
于 2012-06-23T12:48:00.097 回答