Swift for Xojo Developers - Part 9 : Modal MsgBox/InputBox

One of the features that Xojo has is a Modal MsgBox/MessageDialog, that provides information to the user and waits for a response. The standard UIAlertViewController in Swift is what is “normally” used for this function, but requires a lot of code to set up the buttons, the action events etc. But one of the things I didn’t like was if you had more that two buttons, it STACKED them. My class leaves them Horizontal

In addtion, there is an INPUTBOX version with a single TextField

I will not be posting all the code for this class as it exceeds 740+ lines. And while this is a lot of code, you just add the class to a project and forget about it.

  • Does not require declared instances of the MsgBox class on each ViewController that needs to use it, and only requires the Delegate Protocol to be declared if the Msgbox will be used for more than notifications (ie. just an OK button)
  • Provides only TWO action delegates… ‘action’ which indicates what button, and ‘getInput’ to return value of an InputBox [both are optional]
  • Colors, fonts, borders etc. are 100% customizable so you can skin the MsgBox to meet the theme of your App (this is what makes up a bulk of the code to be honest)

Example 1

theMsgBox.caption = "This is the Title"
theMsgBox.message = "This is a\nmultiline message."
theMsgBox.buttons = .OkCancel
theMsgBox.showAlert("boxid")

.

Example 2

theMsgBox.caption = "This is the Title"
theMsgBox.message = "Enter Players Name"
theMsgBox.buttons = .InputBox
theMsgBox.inputText = "dave"
theMsgBox.format    = .Anything // default value
theMsgBox.dismissOnRETURN = true
theMsgBox.showAlert("input")

MsgBox Image 1.

  • .format allows inputbox to filter input.
    • .Anything = accepts, well anything, this is the default
    • .AnyInteger = accepts, only Integer value [digits,+,-]
    • .AnyDouble = accepts,Double value [digits,+,- and . ]
    • .PosInteger = accepts, only Positive Integer value [digits only]
    • .PosDouble = accepts, Positive Double value [digits and . ]
  • .buttons define which buttons are displayed [OK is default if not specified], and yes, these were borrowed from VB6 :slight_smile:
    • .OKOnly
    • .OKCancel
    • .AbortRetryIgnore
    • .YesNoCancel
    • .YesNo
    • .RetryCancel
    • .InputBox - specifies Inputbox with Cancel and OK buttons

In addition to .buttons there is .custom that allows up to 3 buttons with captions of the developers choosing.

theMsgBox.caption="GAME OVER"
theMsgBox.message="There are no move moves."
theMsgBox.custom=["Play Again?","Restart this Game","Exit"]
theMsgBox.showAlert("GAMEOVER")

.
This also shows how it can be skinned… Something you cannot do with UIAlertViewController

3 Likes

Nice one. Is the full code downloadable somewhere?

send me your email address…

I’ll put together a demo program as well…

Note : this (as all controls I write) is 100% Swift code… NO INTERFACE BUILDER!

www.rdsisemore.com/swift_msgbox.zip

Consists of the Msgbox Class module and a UVIewController to test it with

2 Likes

Thanks. I’ll send a pm when I have a bit more time.

Uh… Markus… look up :slight_smile: I posted a link to the code already :smiley:

1 Like

Merci! :+1: