Kim HJ
Kim HJ

Reputation: 1192

UIAlertView deprecated Xamarin iOS

I'm trying to change the following code to the UIAlertController but I get an error saying "Non-invocable member 'UIAlertController' cannot be used like a method"

This is the original code in AppDelegate.cs

private void remoteNotificationPayload(NSDictionary userInfo)
    {
        var aps_d = userInfo["aps"] as NSDictionary;
        var alert_d = aps_d["alert"] as NSDictionary;
        var body = alert_d["body"] as NSString;
        var title = alert_d["title"] as NSString;

        this.showAlert(title, body);
    }

private void showAlert(string title, string message)
{
  var alert = new UIAlertView(title ?? "Title", message ?? "Message", null, "Cancel", "OK");
  alert.Show();
}

I tried this that is where I get the error:

private void showAlert(string title, string message)
{
 var okCancelAlertController = UIAlertController(title ?? "Title", message ?? "Message", preferredStyle: UIAlertControllerStyle.Alert);

 okCancelAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, alert => Console.WriteLine("Okay was clicked")));

 okCancelAlertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, alert => Console.WriteLine("Cancel was clicked")));
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(okCancelAlertController, true, null);
}

Thanks for any help.

Upvotes: 2

Views: 2413

Answers (1)

SushiHangover
SushiHangover

Reputation: 74134

Use the static Create method on UIAlertController:

var okCancelAlertController = UIAlertController.Create(title, message, UIAlertControllerStyle.Alert);

Upvotes: 7

Related Questions