是/否确认UIAlertView

时间:2013-10-01 16:01:11

标签: xamarin.ios uialertview xamarin-studio

我通常使用以下代码进行确认提醒

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事件似乎永远不会被触发。有没有人有一个如何做确认警报的工作示例?

6 个答案:

答案 0 :(得分:34)

Alex Corrado写了这个漂亮的样本,你可以等待使用:

// 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");

答案 1 :(得分:3)

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

此外,作为奖励,我建议您使用buttonClicked而不是{{1}}。看一看,真棒!

答案 2 :(得分: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
                    }
                };

答案 3 :(得分: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;
}

另外,我不明白为什么你要按下按钮,因为你会把事件解雇。但我没有所有的背景。

答案 4 :(得分:1)

请尝试以下适用于我的代码。

new UIAlertView(title, "Are you Sure ",null,"Cancel","Yes").Show();

答案 5 :(得分: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)