Modal UIAlertView (iOS 7)

It’s common practice to use NSRunLoop to show modal UIAlertView to block code execution until user input is processed in iOS.

The below code worked perfectly on previous versions of iOS (5 & 6)

int ShowModalAletView (string title, string message, params string[] buttons)
{
    int clicked = -1;
    var alertView = new UIAlertView (title, message, null, null, buttons);
    alertView.Show ();
    bool done = false;
    alertView.Clicked += (sender, buttonArgs) => {
        Console.WriteLine ("User clicked on {0}", buttonArgs.ButtonIndex);
        clicked = buttonArgs.ButtonIndex;
    };
    while (clicked == -1){
        NSRunLoop.Current.RunUntil (NSDate.FromTimeIntervalSinceNow (0.5));
    }
    return clicked;
}

until Apple released the iOS7, it broke few existing apps 1. Fortunately, there is a better way of implementing it with C#’s async/await

Here’s the new code taking advantage of async/await pattern to show modal UIAlertView:

Task ShowModalAletViewAsync (string title, string message, params string[] buttons)
{
    var alertView = new UIAlertView (title, message, null, null, buttons);
    alertView.Show ();
    var tsc = new TaskCompletionSource ();

    alertView.Clicked += (sender, buttonArgs) => {
        Console.WriteLine (“User clicked on {0}”, buttonArgs.ButtonIndex);
        tsc.TrySetResult(buttonArgs.ButtonIndex);
    };
    return tsc.Task;
}

//Usage
async Task PromptUser() {
    var result = await ShowModalAletViewAsync (“Alert”, “Do you want to continue?”, “Yes”, “No”); //process the result
}

  1. From Apple Developer Forum: https://devforums.apple.com/message/887792

3 thoughts on “Modal UIAlertView (iOS 7)”

  1. Ok, I want to call Popup.Show (“Good job!”);

    the call would be something like… What?

    You leave out any way to make it modal in your example, at least, as far as I can tell.

    I should be able to popup multiple modal boxes by calling the thing multiple times in a row, but how?

    PromptUser();
    PromptUser();
    Do something with value created in PromptUser? – No, they return instantly right?

  2. I want to be able to have the top most method be something simple like:

    public static void Show (string Message)
    {
    // Call your magic
    }

  3. Ok, I think I figured it out. You have to have async keyword on all the methods going up above this one, and call await on the show. In my case, that required adding async keyword to the anonymous function that I had for the event handler of the menu that I pressed the button for. (It then calls await MessageBox.Show () and that puts up the messagebox. A lot of screwing around for iOS 7, but then, that is what iOS 7 is – a lot of screwing around for nothing. (Sigh)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s