Hobbit7
Hobbit7

Reputation: 333

Issue with PresentViewController. How can I use alerts?

I want to show a simple alert in my application but I don't know how to do that. I get this error message when I want to create an alert:

Error CS0103: The name 'PresentViewController' does not exist in the current context

    public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
        //Create Alert
        var okAlertController = UIAlertController.Create("Error", "Error registering push notifications", UIAlertControllerStyle.Alert);

        //Add Action
        okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

        // Present Alert
        PresentViewController(okAlertController, true, null);
    }

How can I use PresentViewController?

Upvotes: 1

Views: 590

Answers (1)

Jason
Jason

Reputation: 89117

you need to get the currently active ViewController first

var window= UIApplication.SharedApplication.KeyWindow;
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
{
    vc = vc.PresentedViewController;
}

//Create Alert
var okAlertController = UIAlertController.Create("Error", "Error registering push notifications", UIAlertControllerStyle.Alert);

//Add Action
okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));

// Present Alert
vc.PresentViewController(okAlertController, true, null);

Upvotes: 4

Related Questions