iOS Guides | Samples

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


[MonoTouch.Foundation.Register("UIAlertController", true)]
[MonoTouch.ObjCRuntime.Introduced(MonoTouch.ObjCRuntime.PlatformName.iOS, 8, 0, MonoTouch.ObjCRuntime.PlatformArchitecture.None, null)]
public class UIAlertController : UIViewController, IUISpringLoadedInteractionSupporting, IDisposable


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

The UIAlertController is a 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 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


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

The members of MonoTouch.UIKit.UIAlertController are listed below.

See Also: UIViewController

Public Constructors

Default constructor, initializes a new instance of this class.
A constructor that initializes the object from the data stored in the unarchiver object.
Constructor to call on derived classes to skip initialization and merely allocate the object.
A constructor used when creating managed representations of unmanaged objects; Called by the runtime.
Creates a new alert controller from the named NIB in the provided bundle.

Public Properties

ActionsUIAlertAction[]. Gets an array of UIAlertAction actions that this UIAlertController object provides to the user.
ClassHandleIntPtr. The handle for this class.
MessageString. Gets or sets a description of the reason for the alert that is associated with this UIAlertController object.
PreferredActionUIAlertAction. Gets or sets the action that will be highlighted when the alert is presented to the user.
PreferredStyleUIAlertControllerStyle. Gets the preferred UIAlertControllerStyle style for that alert that is associated with this UIAlertController object, either an action sheet or an alert.
TextFieldsUITextField[]. Gets an array of UITextField fields to display for the alert that is associated with this UIAlertController object.
TitleString. Gets the title to display for the alert that is associated with this UIAlertController object.

Public Methods

Adds a UIAlertAction action that this UIAlertController displays as a button for the user.
Adds a UITextField object to this UIAlertController to display to the user, and also includes a configurationHandler that the app developer can use to change the text field properties.
Create(String, String, UIAlertControllerStyle) : UIAlertController
Creates a UIAlertController object to display an alert to the user that includes a title, a message, and UIAlertControllerStyle preferred style, and then returns that UIAlertController object.