I have an issue. I want to remove presented view controller from memory. Let's say I have VC1 and I want to present VC2. The presented view controller (VC2) is on Visible View Controller. The presented view controller is failed to add on the view hierarchy because on ViewDidLoad has an error. So, the presented View Controller is stuck and whenever I go to another page the Visible View Controller is still appear , the instance is same and not removed. I want to remove the presented View Controller (VC2) from the memory So when user got the error then user still can navigate to another page without feeling stuck. Any advice is appreciated :)

So far, What I've tried..

#1. I already try to dismissVC but the VC2 is still on the Visible View Controller

#2. I already try to create new navigation controller and set the view controllers using old view controllers but still stuck and the other page become not responsive

My full code:

VC1 :

 public class ViewController1 : UIViewController
{
    public ViewController1() : base()
    {
    }

    private UIButton m_btnReference;

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        View.BackgroundColor = UIColor.White;

        m_btnReference = new UIButton();
        m_btnReference.TranslatesAutoresizingMaskIntoConstraints = false;
        m_btnReference.BackgroundColor = UIColor.Gray;
        m_btnReference.SetTitle("Clickkk!", UIControlState.Normal);
        m_btnReference.TouchUpInside += HandleButtonReferenceClicked;
        View.AddSubview(m_btnReference);
        View.AddConstraint(NSLayoutConstraint.Create(m_btnReference, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1, 10));
        View.AddConstraint(NSLayoutConstraint.Create(m_btnReference, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, -10));
        View.AddConstraint(NSLayoutConstraint.Create(m_btnReference, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 100));
        View.AddConstraint(NSLayoutConstraint.Create(m_btnReference, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View.SafeAreaLayoutGuide, NSLayoutAttribute.Top, 1, 0));
    }

    private void HandleButtonReferenceClicked(object sender, EventArgs e)
    {
        try
        {
            var _getTopVC = GetTopVC(UIApplication.SharedApplication.KeyWindow.RootViewController);

            ViewController2 _nextVC = new ViewController2();
            _nextVC.ModalPresentationStyle = UIModalPresentationStyle.Custom;
            _nextVC.ModalPresentationCapturesStatusBarAppearance = true;


            if (_getTopVC.GetType().Equals(typeof(ViewController2)))
            {
                _getTopVC.DismissViewController(false, null);
            }

            _getTopVC.PresentViewController(_nextVC, true, null);


        }
        catch (Exception ex)
        {
            throw new Exception($"Error! because {ex}");

        };
    }

    private UIViewController GetTopVC(UIViewController aRootController)
    {
        UIViewController topController = UIApplication.SharedApplication.KeyWindow.RootViewController;
        while (topController.PresentedViewController != null)
        {
            topController = topController.PresentedViewController;
        }
        return topController;
    }

    public override void ViewDidDisappear(bool animated)
    {
        base.ViewDidDisappear(animated);
        Console.WriteLine("DidDisappear called!");
    }

    public override void ViewWillDisappear(bool animated)
    {
        base.ViewWillDisappear(animated);
        Console.WriteLine("WillDisappear called!");
    }
}

VC2:

 public class ViewController2 : UIViewController
{
    public ViewController2() : base()
    {
    }

    private UITextView tvText;
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();

        View.BackgroundColor = UIColor.White;

        tvText.Text = "Tessssssss";
        tvText.TranslatesAutoresizingMaskIntoConstraints = false;
        tvText.BackgroundColor = UIColor.Gray;
        View.AddSubview(tvText);
        View.AddConstraint(NSLayoutConstraint.Create(tvText, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1, 0));
        View.AddConstraint(NSLayoutConstraint.Create(tvText, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, 0));
        View.AddConstraint(NSLayoutConstraint.Create(tvText, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View.SafeAreaLayoutGuide, NSLayoutAttribute.Top, 1, 0));
        View.AddConstraint(NSLayoutConstraint.Create(tvText, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View.SafeAreaLayoutGuide, NSLayoutAttribute.Bottom, 1, 0));

    }

    public override void ViewDidDisappear(bool animated)
    {
        base.ViewDidDisappear(animated);
        Console.WriteLine("DidDisappear called!");
    }

    public override void ViewWillDisappear(bool animated)
    {
        base.ViewWillDisappear(animated);
        Console.WriteLine("WillDisappear called!");
    }
}

Thank you for your time!

EDit #1:

I added UIAlertVC when the catch exception is on progress. But, the dismiss progress still not working

catch (Exception ex)
        {
            UIAlertController _alertController = UIAlertController.Create("Error", ex.ToString(), UIAlertControllerStyle.Alert);
            _alertController.AddAction(UIAlertAction.Create(title: "Ok ",
                                                       style: UIAlertActionStyle.Default,
                                                       handler: (obj) => { this.DismissViewController(true, null); }));

            GetTopVC(UIApplication.SharedApplication.KeyWindow.RootViewController).PresentViewController(_alertController, true, null);

        };
0

There are 0 best solutions below