5

我通常使用以下代码进行确认警报

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;
}

这似乎不适用于iOS7。循环继续运行,Clicked事件似乎永远不会被触发。有没有人有一个如何进行确认警报的工作示例?

4

6 回答 6

34

Alex Corrado 编写了这个漂亮的示例,您可以将其与 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;
}

然后你改为:

int button = await ShowAlert ("Foo", "Bar", "Ok", "Cancel", "Maybe");
于 2013-10-03T14:44:30.717 回答
3

经典之一!您在添加事件处理程序之前显示警报视图。

另外,作为奖励,我建议您使用Async/Await 功能,而不是使用buttonClicked. 看看,太棒了!

于 2013-10-02T07:33:13.317 回答
2

可以尝试这样的事情希望它有所帮助

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
                    }
                };
于 2014-03-25T07:15:58.747 回答
1

Clicked在调用之前尝试将您的处理程序附加到Show()事件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;
}

另外,我不明白你为什么要等待按下按钮,因为你会触发事件。但我没有所有的上下文。

于 2013-10-02T08:47:58.383 回答
1

UIAlertView已从 iOS 8.0 弃用,因此应该使用一个好的解决方案UIAlertViewController

let alert = UIAlertController(title: "message", message: "Title", preferredStyle: .Alert)

alert.addAction(UIAlertAction(title: "YES", style: .Default, handler: { (action) -> Void in
    // Action for YES
}))
alert.addAction(UIAlertAction(title: "NO", style: .Default, handler: { (action) -> Void in
    // Action for NO
}))
alert.addAction(UIAlertAction(title: "CANCEL", style: .Default, handler: { (action) -> Void in
    // Action for CANCEL
}))
self.view.window!.rootViewController!.presentViewController(alert, animated: true, completion: nil)
于 2016-03-28T11:39:03.633 回答
1

尝试以下对我有用的代码。

new UIAlertView(title, "Are you Sure ",null,"Cancel","Yes").Show();
于 2015-09-03T07:31:28.120 回答