In this example I will show you how to make an alert appear in a Swift based iOS application. Very straight forward and something you are going to need at some point. For this example I will make an alert appear showing the text entered as the message along with two possible actions, one clears the text and the other is a simple cancel button.
First, I created a new single view application, I added text entry and button controls to the screen also creating an @IBOutlet and @IBAction for both controls.
The alert view and action sheet both use the UIAlertController class, the class will provide you an instance that you use to present your alert. So let's create an instance when the user touches the button.
var alertController:UIAlertController?
alertController = UIAlertController(title: "Alert",
message: textEntryField.text,
preferredStyle: .Alert)
There are some straightforward arguments to complete of which the first one speaks for it's self. The second is the message, I simply assign the current value of the text entry field and the third is the style we want in this case .Alert.
Now we need to add any actions that we want to the alert view (or action sheet!), these would normally be buttons, so we create instances of the UIAlertAction class and use the addAction: method to strangely enough add them! The first action will simply do nothing which makes the alert disappear, the second will clear the contents of the text entry field.
// The cancel button
let firstAction = UIAlertAction(title: "OK",
style: UIAlertActionStyle.Cancel,
handler: nil
)
// The clear text button
let secondAction = UIAlertAction(title: "Clear Text",
style: UIAlertActionStyle.Default,
handler: {(paramAction:UIAlertAction!) in
selt.textEntryField.text = ""
})
The final stage in preparing the alert is to add the actions to the alert.
alertController!.addAction(firstAction)
alertController!.addAction(secondAction)
Then finally, we need to present the alert view as the last part of the code when the button is touched. We do this by telling the current viewController self to present it.
self.presetViewController(alertController!,
animated: true,
completion: nil)
So the entire contents of the button action should look like this
@IBAction func btnDisplayAlert(sender: AnyObject) {
var alertController:UIAlertController?
alertController = UIAlertController(title: "Alert",
message: textEntryField.text,
preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Cancel",
style: UIAlertActionStyle.Cancel,
handler: nil
)
let secondAction = UIAlertAction(title: "Clear Text",
style: UIAlertActionStyle.Default,
handler: {(paramAction:UIAlertAction!) in
self.textEntryField.text = ""
})
alertController!.addAction(firstAction)
alertController!.addAction(secondAction)
self.presentViewController(alertController!,
animated: true,
completion: nil)
}
Now run the application and enter some text and touch the button, you should see the sheet appear with the two possible options.
Try a button then the other.
Hopefully this short introduction to displaying an alert has given you everything you need to get started, as with everything iOS there are many ways and many uses for things, play around and see what you can come up with.
I also have a post showing how to ‘Display an Alert with Text Field Entry.'
Very helpful, thank you very much! I was able to easily use this tutorial to add a function that is not associated with an IBAction to make Warnings.
Hi Craig,
Thank you, I am so glad to read that it helped you. I plan to share more that I hope will help just as much.