Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.ios 是/否确认视图_Xamarin.ios_Uialertview_Xamarin Studio - Fatal编程技术网

Xamarin.ios 是/否确认视图

Xamarin.ios 是/否确认视图,xamarin.ios,uialertview,xamarin-studio,Xamarin.ios,Uialertview,Xamarin Studio,我通常将以下代码用于确认警报 int按钮点击=-1; UIAlertView alert=new UIAlertView(标题、消息、空、NSBundle.MainBundle.LocalizedString(“取消”、“取消”), NSBundle.MainBundle.LocalizedString(“OK”、“OK”); alert.Show(); alert.Clicked+=(发件人,buttonArgs)=>{buttonClicked=buttonArgs.ButtonIndex;

我通常将以下代码用于确认警报

int按钮点击=-1;
UIAlertView alert=new UIAlertView(标题、消息、空、NSBundle.MainBundle.LocalizedString(“取消”、“取消”),
NSBundle.MainBundle.LocalizedString(“OK”、“OK”);
alert.Show();
alert.Clicked+=(发件人,buttonArgs)=>{buttonClicked=buttonArgs.ButtonIndex;};
//等待按钮按下。
while(按钮点击==-1)
{
NSRunLoop.Current.runtill(NSDate.FromTimeIntervalSinceNow(0.5));
}
如果(按钮点击==1)
{
返回true;
}
其他的
{
返回false;
}

这在iOS7中似乎不起作用。循环继续运行,单击的
事件似乎永远不会被触发。有没有人有一个如何发出确认警报的工作示例?

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


另外,作为奖励,我建议您使用,而不是使用单击的
按钮。看一看,太棒了

在调用
UIAlertView
上的
Show()
之前,尝试将处理程序附加到单击的
事件:

int按钮点击=-1;
UIAlertView alert=new UIAlertView(标题、消息、空、NSBundle.MainBundle.LocalizedString(“取消”、“取消”),
NSBundle.MainBundle.LocalizedString(“OK”、“OK”);
alert.Clicked+=(发件人,buttonArgs)=>{buttonClicked=buttonArgs.ButtonIndex;};
alert.Show();
//等待按钮按下。
while(按钮点击==-1)
{
NSRunLoop.Current.runtill(NSDate.FromTimeIntervalSinceNow(0.5));
}
如果(按钮点击==1)
{
返回true;
}
其他的
{
返回false;
}

还有,我不明白你为什么要等按钮按下,因为你会被触发事件。但我不知道所有的上下文。

亚历克斯·科拉多(Alex Corrado)写了这个漂亮的例子,你可以用它来等待:

//显示UIAlertView并返回所按下按钮的索引。
公共静态任务ShowAlert(字符串标题、字符串消息、参数字符串[]按钮)
{
var tcs=new TaskCompletionSource();
var-alert=新的UIAlertView{
头衔,
消息=消息
};
foreach(按钮中的var按钮)
alert.AddButton(按钮);
alert.Clicked+=(s,e)=>tcs.TrySetResult(e.ButtonIndex);
alert.Show();
返回tcs.Task;
}
然后你会:

int按钮=等待显示警报(“Foo”、“Bar”、“Ok”、“Cancel”、“Maybe”);

可以尝试类似的方法,希望对您有所帮助

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
                    }
                };
试试下面的代码,它对我有用


iOS 8.0不推荐使用
UIAlertView
,因此一个好的解决方案应该是使用
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)

看起来像是打字错误,我的代码在订阅活动后显示了消息框,我仍然有同样的问题。嘿,我没有得到你在这里推荐的东西,你能给我一个小的代码片段让我有个清晰的想法吗?@susant Icaza的答案正是我推荐的:)请注意,e.ButtonIndex不会在我的机器上编译,因为TrySetResult正在寻找一个nint。您需要在那里将其强制转换为nint,或者将任务更改为Task
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)