在Xamarin.iOS中以编程方式解散UIAlertView

问题描述 投票:0回答:2

我陷入了一个奇怪的场景,我需要通过获取实例以编程方式从其他viewcontroller中解除活动的UIAlertView。

实际上,我正在研究AutoClose功能(在System.Timer上工作),当Timer过去时,我需要执行一些操作(在View Model中执行)并关闭活动的UIAlertView或UIViewController窗口并导航到HomeView。

我能够使用UIViewController,但我找不到任何方法来解除从另一个视图控制器呈现的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中看到Views的层次结构?

xamarin.ios
2个回答
0
投票

尝试使用以下代码:

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

如果您想要关闭警报视图(对于以下代码中的emample,我会在3秒后将其关闭):

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

0
投票

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

Dismiss UIAlertView programmatically in Xamarin.iOS

我希望这可以帮助别人。

© www.soinside.com 2019 - 2024. All rights reserved.