0

forオブジェクトの配列を反復処理するループを作成しました。IBAction今、私は、ユーザーが?を呼び出すボタンをクリックするまで、ループの反復を中断することが可能かどうかを自問しています。

for (int i = 0; i < [array count]; i++) {
   // do something with the object

   // wait for action method called
   // user clicked action so go on
}
4

2 に答える 2

4

ケースに合わせてコードを調整できます。基本的に、ループを複数のメッセージに「展開」します。シーケンスを開始します[self doItForIndex:[NSNumber numberWithInt:0]];

- (BOOL)canDoitForIndex:(NSNumber *)i {
    // return YES if you want to go ahead
    // (e.g. test a BOOL you set in response to the user tapping a button
}

- (void)waitForIndex:(NSNumber *)i {
    if ([self canDoItForIndex:i]) {
        // do anything to clean up for i
        // then repeat for i+1:
        [self doItForIndex:[NSNumber numberWithInt:[i intValue]+1]];
    } else {
        [self performSelector:_cmd withObject:i afterDelay:0.01f;
    }
}

- (void)doItForIndex:(NSNumber *)i {
    if ([i intValue] < lastIndex) {
        // do what you have to do
        [self waitForIndex:i];
    }
    // else you're done
}

AppleのNSRunLoopコンセプトは、処理をかなり迅速に完了することを期待しています。何かを待ってメインスレッドを拘束すると、アプリ内で他に何も起こりません。上記のコードは、「待機」を複数のメッセージ送信に分割し、アプリの応答性を維持します。

于 2011-04-30T17:07:48.120 に答える
1

ODRMアルゴリズムは非常にうまく機能します。この行を変更しました:

[self performSelector:_cmd withObject:i afterDelay:0.01f];

これとともに :

   [NSThread sleepForTimeInterval:0.25];  
   [NSThread detachNewThreadSelector:_cmd toTarget:self withObject:i];

UI要素を更新する必要があったので、バックグラウンドスレッドで待機するように強制する方が適切でした。

于 2012-03-11T17:51:03.477 に答える