C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: In iOS and the iOS simulator, the alert is displayed over a dimmed view of the screen contents.
Example ViewController: Swift
//
// ViewController.swift
// ExampleTime5
//
// ...
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBOutlet weak var simpleButton: UIButton!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
So: Right click on the Button, and drag from the circle next to "Primary Action Triggered" to your ViewController file in Swift.
Title: This is the large, bold text in the alert box. It is shown near the main text.
Message: This text is smaller and comes below the title text. It should contain more details.
PreferredStyle: To create an alert box, please us the UIAlertControllerStyle.Alert value for the preferredStyle.
PresentViewController: This shows the alert box. We can pass nil for completion, but usually a block is code is needed.
Example ViewController, button: Swift
//
// ViewController.swift
// ExampleTime5
//
// ...
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func actionTriggered(sender: AnyObject) {
// Create a UIAlertController.
// ... Use Alert style.
let dialog = UIAlertController(title: "Hello",
message: "How are you?",
preferredStyle: UIAlertControllerStyle.Alert)
// Present the dialog.
// ... Do not worry about animated or completion for now.
presentViewController(dialog,
animated: false,
completion: nil)
}
@IBOutlet weak var simpleButton: UIButton!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}