0

I'm trying to implement an error popup function for iOS. My current implementation:

void SysErrorAlert(NSString * title, NSString * message, ...)
{
    NSString * contents = nil;

    va_list args;
    va_start(args, message);
    contents = [[NSString alloc] initWithFormat:message arguments:args];
    va_end(args);

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title
                                                     message:contents
                                                    delegate:nil
                                           cancelButtonTitle:@"Cancel"
                                           otherButtonTitles:@"OK", nil];
    [alert show];

    // tried this but popup still never shows...
    //for (;;) { }
}

However, "[alert show]" is returning immediately without ever displaying the popup dialog. I need the dialog to be displayed on top of the current application screen and block the calling thread until the user clicks one of the buttons. The application will them terminate after the function returns.

The app is running Cocos2d, so maybe the Cocos drawing is interfering with the UIAlertView... But I'm rather new to iOS programming and may be missing something obvious here.

NOTE: I have not tested this on an actual device, only in the simulator. Could it be a limitation/bug of the simulator?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
glampert
  • 4,371
  • 2
  • 23
  • 49
  • 1
    FYI - `[alert show]` is supposed to return immediately. Obviously there is a problem somewhere, but `show` returning immediately is normal. It is not designed to block the thread. You need to program for event handling. – rmaddy Mar 29 '14 at 00:02
  • So what is the usual way of implementing a blocking message dialog? Something similar to Window's MessageBox()? – glampert Mar 29 '14 at 00:05
  • 1
    Your code *should* work. Could it be a threading issue? Normally when things don't appear when they should its because some UI code was executed on a background queue (for me anyways). – Mike D Mar 29 '14 at 00:06
  • Yes @MikeD, it might be related to Cocos2d, but I don't know the library well enough to tell... – glampert Mar 29 '14 at 00:07
  • Be fore you create the alert add `BOOL mainThread = [NSThread isMainThraed];`. You are not on the main thread if `mainThread` is `FALSE/NO`. – Mike D Mar 29 '14 at 00:09
  • 2
    @glampert This isn't Windows. There is no blocking message dialog. You show the alert and finish. You implement the proper `UIAlertViewDelegate` methods and your code will be called when the user taps a button. You then handle that as needed. – rmaddy Mar 29 '14 at 00:11
  • @MikeD, does UIAlertView has to de called on the main thread? Furthermore, does 'show' display the message dialog immediately after returning, or the application has to go thru an iteration of the run loop? – glampert Mar 29 '14 at 02:57
  • 1
    @glampert Yes, all UI related code must be called on the main thread. And yes, the app needs to get back to the top of the run loop for the alert view to actually appear. – rmaddy Mar 29 '14 at 04:58
  • Then I don't think UIAlertView will do. The object of the function I'm writing is to display a fatal error message. After the function returns, the application may be in an undefined state and may need to be terminated. That is the reason why I need the message to be displayed immediately. Would this be possible on iOS? – glampert Mar 29 '14 at 17:44
  • 1
    @glampert It sounds like you are trying to implement and execption handler. This should be a good starting point: http://stackoverflow.com/q/1787254/620197 – Mike D Mar 29 '14 at 18:49

1 Answers1

1

Looks like you have to ask cocos2d for help to get the right parent for the alertview This older post suggests an outline: http://www.cocos2d-iphone.org/forums/topic/how-to-popup-a-uialertview-with-cocos2d/

Dylan
  • 530
  • 2
  • 11
  • 5
    Having a `nil` delegate would not cause the problem. It prevents you from handling the buttons of course, but it won't prevent the alert from appearing. – rmaddy Mar 29 '14 at 00:10