Yes/No Confirmation UIAlertView

12,903

Solution 1

Alex Corrado wrote this beautiful sample that you can use with await:

// Displays a UIAlertView and returns the index of the button pressed.
public static Task<int> ShowAlert (string title, string message, params string [] buttons)
{
    var tcs = new TaskCompletionSource<int> ();
    var alert = new UIAlertView {
        Title = title,
        Message = message
    };
    foreach (var button in buttons)
        alert.AddButton (button);
    alert.Clicked += (s, e) => tcs.TrySetResult (e.ButtonIndex);
    alert.Show ();
    return tcs.Task;
}

Then you do instead:

int button = await ShowAlert ("Foo", "Bar", "Ok", "Cancel", "Maybe");

Solution 2

Classic one! You're showing the alert view before adding the event handler.

Also, as a bonus, I'd recommend you to use the Async/Await features instead using buttonClicked. Take a look, it's awesome!

Solution 3

Could try something like this hope it helps

var Confirm  = new UIAlertView("Confirmation", "Are you Sure ",null,"Cancel","Yes");
                Confirm.Show();
                Confirm.Clicked += (object senders, UIButtonEventArgs es) => 
                {
                    if (es.ButtonIndex == 0 ) {
                                           // do something if cancel
                    }else
                    {
                        // Do something if yes
                    }
                };

Solution 4

Try to attach your handler to the Clicked event before calling Show() on the UIAlertView:

int buttonClicked = -1; 
UIAlertView alert = new UIAlertView(title, message, null, NSBundle.MainBundle.LocalizedString ("Cancel", "Cancel"),
                                NSBundle.MainBundle.LocalizedString ("OK", "OK"));
alert.Clicked += (sender, buttonArgs) =>  { buttonClicked = buttonArgs.ButtonIndex; };
alert.Show ();

// Wait for a button press.
while (buttonClicked == -1)
{
    NSRunLoop.Current.RunUntil(NSDate.FromTimeIntervalSinceNow (0.5));
}

if (buttonClicked == 1)
{
    return true;
}
else
{
    return false;
}

Also, I don't get why you wait for a button press, as you'll got the event fired. But I don't have all the context.

Solution 5

Try following code it works for me.

new UIAlertView(title, "Are you Sure ",null,"Cancel","Yes").Show();
Share:
12,903
JonBull2013
Author by

JonBull2013

Updated on June 15, 2022

Comments

  • JonBull2013
    JonBull2013 almost 2 years

    I usually use the following code for a confirmation alert

    int buttonClicked = -1;
    UIAlertView alert = new UIAlertView(title, message, null, NSBundle.MainBundle.LocalizedString ("Cancel", "Cancel"),
                                        NSBundle.MainBundle.LocalizedString ("OK", "OK"));
    alert.Show ();
    alert.Clicked += (sender, buttonArgs) =>  { buttonClicked = buttonArgs.ButtonIndex; };
    
    // Wait for a button press.
    while (buttonClicked == -1)
    {
        NSRunLoop.Current.RunUntil(NSDate.FromTimeIntervalSinceNow (0.5));
    }
    
    if (buttonClicked == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
    

    This doesn't appear to be working in iOS7. The loop just continues to run and the Clicked event never seems to get fired. Does anyone have a working example of how to do a confirmation alert?

  • JonBull2013
    JonBull2013 over 10 years
    Looks like a typo, my code shows the message box after subscribing to the event and I still have the same issue.
  • susant
    susant over 9 years
    Hey i am not getting what you are recommend over here, Can u give a small code snippet to have clear idea....
  • kbtz
    kbtz over 9 years
    @susant Icaza's answer is exactly what I've recommended :)
  • Daniel Casserly
    Daniel Casserly almost 9 years
    Just a note on this that the e.ButtonIndex will not compile on my machine as the TrySetResult is looking for a nint. You either need to cast it to nint there or change the Task to be a Task<nint>