Alert


super: UIAlertController (on iOS)

The Alert class displays a customizable alert message to the user.

Events

  • Load() This event is called when the object becames available in the current runtime system.

  • WillShow() Use this event to be notified when the Alert is about to be added to a view hierarchy.

  • DidShow() Use this event to be notified when the Alert was added to a view hierarchy.

  • Action(index: Int, title: String) Use this event to be notified when user selects a button.

  • WillHide() Use this event to be notified when the Alert is about to be removed from a view hierarchy.

  • DidHide() Use this event to be notified when the Alert was removed from a view hierarchy.

  • Unload() This event is called when the object has been removed from the current runtime system (but not yet deallocated).

Constructors

  • func Alert(title: String) Create a new Alert message with a specified title.

  • func Alert(title: String, message: String) Create a new Alert message with a specified title and message.

  • func Alert(title: String, message: String, buttons: List) Create a new Alert message with a specified title and message plus an array of buttons titles.

  • func Alert(title: String, message: String, buttons: List, completion: Closure) Create a new Alert message with a specified title and message plus an array of buttons titles. The completion closure (if set) enables you to specify an action to be executed when user press a button (button index is passed as parameter).

Methods

  • func addActionWithTitle(title: String, style: Int = 0) Use this method to add a button to a newly created Alert.

  • func show() Display Alert to the user.

Examples

A simple Alert:

var alert = Alert("Title", "Hello, I am an Alert.");
alert.show();

Result: Simple Alert

A two buttons Alert:

var alert = Alert("Title", "Hello, I am an Alert.", ["Cancel", "OK"]);
alert.show();

Result: Two buttons Alert

A three buttons Alert:

var alert = Alert("Title", "Hello, I am an Alert. Are you ready?", ["Maybe", "Yes", "No"]);
alert.show();

Result: Three buttons Alert

Use a callback to check for button pressed:

var callback = func (index) {
    var alert = Alert("", "You pressed button \(index).")
    alert.show();
}

var alert = Alert("Title", "Hello, I am an Alert. Are you ready?", ["Maybe", "Yes", "No"], callback);
alert.show();

Result: Complex Alert