0
votes

I am stuck in a weird scenario where I need to programmatically dismiss the active UIAlertView from the other viewcontroller by getting the instance.

Actually, I am working on AutoClose feature (working on System.Timer) where when Timer would be elapsed, I need to perform some operation (performs in View Model) and close the active UIAlertView or UIViewController window and Navigate To HomeView.

I am able to do with UIViewController but I couldn't find any way to dismiss the UIAlertView which is presented from another view controller?

Note - I understand this API is deprecated but for using UIAlertController it would lead to ample code changes, and I don't have time to make them.

I am using below method to accomplish but once the AlertView gets dismissed I am unable to click anywhere on screen.

UIWindow window = UIApplication.SharedApplication.KeyWindow;
UIViewController rootViewController=window.RootViewController;

if(rootViewController.ModalViewController is UIAlertController)
{
    rootViewController.ModalViewController.DismissViewController(true,null);    
}

Also, may I know how do we see the hierarchy of Views in Xamarin Studio?

2
Note - I understand this API is deprecated but for using UIAlertController it would lead to ample code changes. So, I would not prefer to do it as of now since it's urgent requirement for me. - JJBHATT

2 Answers

0
votes

Try to use the following code:

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

And if when you want to Dissmiss the AlertView(for emample in the following code I dissmiss it after 3 seconds):

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

I have asked the same question in Xamarin Forum and I got my answer from one of the Xamarin Professional which I am sharing here.

Dismiss UIAlertView programmatically in Xamarin.iOS

I hope this may help others.