views:

168

answers:

0

I've got a custom ModalDialog class, which I've got working well in most situations. It returns its results via a delegate method, which then continues the app appropriately based on the user's button selection.

However, there are some places in my app where the dialog really needs to pop up in the middle of a method (for example, my game allows cheating. During a nested loop of game state analysis, if the app finds that the current move would be cheating, it needs to ask the user if he wants to cheat). There's no convenient way to have the delegate return method jump back into the middle of these nested loops and continue.

What I've come up with is to use NSRunLoop's runMode:beforeDate: in a while loop until the user makes a decision.

What are the drawbacks to this approach? Is there a better way?

    for (x = 1; x <= 12; x++) {
      for (y = 1; y <= 9; y++) { 

        /* complicated game state analysis takes place here */

        if (thisMoveWouldBeCheating) {
          waitingForDialogToDismiss = YES;

          [modalDialog showInView:self.view 
                          title:@"Cheat?"
                        message:@"That move is a cheat and will affect your game stats."
                     cancelText:@"Cancel"
                    proceedText:@"Cheat"];

          while (waitingForDialogToDismiss == YES) {
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                     beforeDate: [NSDate dateWithTimeIntervalSinceNow:0.1]];
          }
        }
      }
    }

Thanks!