Showing a blocked UIAlertView in ViewDidAppear Method on iOS 7 with Xamarin.iOS does not work

倾然丶 夕夏残阳落幕 提交于 2019-12-23 09:38:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!