问题
if I try to show a blocked AlertView in the ViewDidAppear Method on iOS 7, I won´t be able to dismiss the AlertView. No touch events will reach the AlertView. The Button of the View will be highlighted, but nothing happen. If I run the same code on iOS < 7, it will work fine.
Here is the example code:
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
var alert = new UIAlertView("Title", "Message", null, "OK");
alert.Dismissed += (sender, args) => result = true;
alert.Show();
while (!result)
NSRunLoop.Current.RunUntil(NSDate.FromTimeIntervalSinceNow(0.1));
}
I am using Xamarin.iOS Version 6.4.3.0 with Apple SDK 6.1. I have the same problems with alpha of Xamarin.iOS 6.9.6.0 and Apple SDK 7.0.
Can somebody tell me what the problem is?
Thanks for help.
回答1:
It's not good idea to use NSRunLoop
to block UI execution, but certainly there is a better way of doing it.
You can make use async/await pattern to wait for user to press the UIAlertView
button. Here's the sample that you can use: https://gist.github.com/prashantvc/6725882
回答2:
According to this Apple developer forum thread, iOS 7 has a bug where the UIAlertView delegate will never be called once the RunLoop is started. According to one person, it may still work on the iPad. You can subscribe to email updates for that forum thread in case a fix or workaround is found.
回答3:
You should add
alert.Clicked += AlertViewClick;
and do stuff in
private void AlertViewClick (object sender, UIButtonEventArgs ea)
{
//Stuff Here
}
and remove
while (!result)
NSRunLoop.Current.RunUntil(NSDate.FromTimeIntervalSinceNow(0.1));
来源:https://stackoverflow.com/questions/18607349/showing-a-blocked-uialertview-in-viewdidappear-method-on-ios-7-with-xamarin-ios