iOS Message Box

From Xojo Documentation

A MessageBox displays a prompt for the user. Although it is a UI control, it does not appear on the Layout so when you drag it to your Layout, it is added to the Shelf.

Unlike with desktop a dialog (but similar to a web dialog), the iOS Message Box is not modal. Your code does not stop executing when the Message Box is displayed. If you have code you want to run after the Message Box is closed, then put it in the ButtonAction event.

iOS Message Box Library Icon

Below are common events, properties and methods. Refer to iOSMessageBox in the Language Reference for the complete list.



This event is called when one of the buttons on the Message Box is tapped, with a parameter containing the 0-based index of the button that was tapped.



This is the message to display in the Message Box. In the sample shown on the right it is the "This is a sample message." text.


This is the title that appears at the top of the Message Box. In the sample shown on the right it is the "Hello" text.



These are the buttons to display.


This displays the Message Box on the screen.


iOS Message Box

The easiest way to use a Message Box is to drag it from the Library onto the Layout. The Inspector for a Message Box can be used to change the Message, Title, Left Button and Right Button properties. If you need more than two buttons, you can add them using code. When there are more than two buttons, they display vertically. This example adds three buttons to a Message Box that has been added to the View:

Var buttons() As Text

HelloMessage.Buttons = buttons


It is important to understand that your code does not pause at the "Show" call to wait for a button to be tapped. A Message Boxe is asynchronous, which means it displays immediately and your code continues running to the end of the method or event.

Calling the Show method does not pause your code until a button is tapped.

To determine when a button was tapped, you must add the ButtonAction event handler to the Message Box. This event is called with a parameter containing the (0-based) index of the button that was tapped. This code displays the index for the tapped button:

Label1.Text = "You selected button " + buttonIndex.ToText

More Advanced Technique

iOS Message Box Vertical Buttons

You can also add a Message Box entirely in code, but you'll need to provide a method (on the View) to handle the ButtonAction event (using AddHandler). To add a simple Yes/No Message Box, start by adding a property to the Layout:

SampleMessageBox As iOSMessageBox

In the code that displays the Message Box, create it and tell it to call a method when the ActionEvent is called. You might do this on a Button:

SampleMessageBox = New iOSMessageBox
SampleMessageBox.Title = "Hello"
SampleMessageBox.Message = "Hello, World!"

Var buttons() As Text
SampleMessageBox.Buttons = buttons

AddHandler SampleMessageBox.ButtonAction, AddressOf SampleButtonAction


Now add the method to the View to perform an action and remove the handler:

Private Sub SampleButtonAction(sender As iOSMessageBox, index As Integer)
OutputLabel.Text = "Button tapped."

RemoveHandler SampleMessageBox.ButtonAction, AddressOf SampleButtonAction
End Sub

See Also

iOSMessageBox class; UserGuide:iOS Dialog Boxes, UserGuide:iOS UI, UserGuide:iOS Dialog Boxes topics