在 Xamarin.iOS 中以编程方式关闭 UIAlertView



我陷入了一个奇怪的场景,我需要通过获取实例以编程方式从另一个视图控制器中消除活动的UIAlertView。

实际上,我正在研究自动关闭功能(在System.Timer上工作(,当计时器经过时,我需要执行一些操作(在视图模型中执行(并关闭活动的UIAlertView或UIViewController窗口并导航到HomeView。

我能够使用 UIView控制器,但我找不到任何方法来关闭从另一个视图控制器呈现的 UIAlertView?

注意 - 我知道此 API 已弃用,但对于使用 UIAlertController,它会导致大量的代码更改,我没有时间进行更改。

我正在使用以下方法来完成,但是一旦AlertView被关闭,我就无法单击屏幕上的任何地方。

UIWindow window = UIApplication.SharedApplication.KeyWindow;
UIViewController rootViewController=window.RootViewController;
if(rootViewController.ModalViewController is UIAlertController)
{
rootViewController.ModalViewController.DismissViewController(true,null);    
}

另外,我可以知道如何查看 Xamarin Studio 中视图的层次结构吗?

尝试使用以下代码:

UIAlertView alertView = new UIAlertView("Title", "Content",this,"OK");
alertView.Show();
UIActivityIndicatorView indicatorView = new UIActivityIndicatorView();
indicatorView.BackgroundColor = UIColor.Red;
indicatorView.Center = new CGPoint(alertView.Bounds.Size.Width / 2, 
alertView.Bounds.Size.Height - 40.0);
indicatorView.StartAnimating();
alertView.InsertSubview(indicatorView,0);

如果你想关闭 AlertView(在下面的代码中,我在 3 秒后关闭它(:

NSTimer.CreateScheduledTimer(3, (t) =>
{
alertView.DismissWithClickedButtonIndex(0, true);
});

我在Xamarin论坛上问了同样的问题,我从我在这里分享的一位Xamarin Professional那里得到了答案。

在 Xamarin.iOS 中以编程方式关闭 UIAlertView

我希望这可以帮助其他人。

最新更新