MonoTouch.UIKit.UIAlertController Class
Displays an alert message or action sheet to the user, with optional text fields for data entry and action buttons.

See Also: UIAlertController Members

Syntax

[MonoTouch.Foundation.Register("UIAlertController", true)]
[MonoTouch.ObjCRuntime.Availability(Introduced=MonoTouch.ObjCRuntime.Platform.iOS_8_0)]
public class UIAlertController : UIViewController

Remarks

Starting with iOS 8, this class replaces MonoTouch.UIKit.UIActionSheet and MonoTouch.UIKit.UIAlertView.

The UIAlertController is a MonoTouch.UIKit.UIViewController that is intended to be used for presenting alerts to the user.   Once you create your alert controller, you add actions using the UIAlertController.AddAction method or add input fields text using the UIAlertController.AddTextField method and then you present it like you would any other view controller.    

Text fields are only supported with the Alert style.

The following example shows how to configure a simple alarm.   

c# Example

var alert = UIAlertController.Create ("Alarm", "Wake up sleeping giant!", UIAlertControllerStyle.Alert);

alert.AddAction (UIAlertAction.Create ("Ok", UIAlertActionStyle.Cancel, null));
alert.AddAction (UIAlertAction.Create ("Snooze", UIAlertActionStyle.Default, action => Snooze ()));
if (alert.PopoverPresentationController != null)
    alert.PopoverPresentationController.BarButtonItem = myItem;
PresentViewController (alert, animated: true, completionHandler: null);

When using the UIAlertControllerStyle.Alert style, the message would be centered in the screen and look like this:

When using the UIAlertControllerStyle.ActionSheet style, the message would show at the bottom of the screen, and look like this:

When the UIViewController.PopoverPresentationController is not null, this means that the action sheet is being presented in a way that will require you to specify an anchor point, to produce something like this:

To achieve this, you must set one of the following properties to a value:

Those properties will allow the UIViewController.PreserntViewController determine where to display your action sheet.   If you are responding to a tap from a bar button item (like the screenshot above), you can just cast the sender parameter to a UIBarButtonItem, for example:

c# Example

void HandleAddButtonItem(NSObject sender)
{
    var alert = UIAlertController.Create ("Alarm", "Wake up sleeping giant!", UIAlertControllerStyle.Alert);
    if (alert.PopoverPresentationController != null)
        alert.PopoverPresentationController.BarButtonItem = sender as UIBarButtonItem;	

    alert.AddAction (UIAlertAction.Create ("Ok", UIAlertActionStyle.Cancel, action => Ok ()));
    alert.AddAction (UIAlertAction.Create ("Snooze", UIAlertActionStyle.Default, action => Snooze ()));
    PresentViewController (alert, animated: true, completionHandler: null);
}

Other common idioms include using the sender as MonoTouch.UIKit.UIView and setting the UIPopoverPresentationController.SourceView to it, and the UIPopoverPresentationController.SourceRect to the boundaries of that view for example.

When adding text fields, you use the parameter to configure the text field (colors, default text, placeholder text, secure text entry and so on).  

c# Example

UIAlertController alert = UIAlertController.Create ("Login", "Enter your credentials", UIAlertControllerStyle.Alert);

alert.AddAction (UIAlertAction.Create ("Login", UIAlertActionStyle.Default, action => {
    // This code is invoked when the user taps on login, and this shows how to access the field values
    Console.WriteLine ("User: {0}/Password: {1}", alert.TextFields[0].Text, alert.TextFields [1].Text);
}));

alert.AddAction (UIAlertAction.Create ("Cancel", UIAlertActionStyle.Cancel, myCancel));
alert.AddTextField ((field) => {
	field.Placeholder = "email address";
});
alert.AddTextField ((field) => {
	field.SecureTextEntry = true;
});
PresentViewController (alert, animated: true, completionHandler: null);

Related content

Requirements

Namespace: MonoTouch.UIKit
Assembly: monotouch (in monotouch.dll)
Assembly Versions: 0.0.0.0