Swift: Difference between revisions
Jump to navigation
Jump to search
Line 34: | Line 34: | ||
@IBOutlet weak var ageTextField: UITextField! | @IBOutlet weak var ageTextField: UITextField! | ||
@IBOutlet weak var helloLabel: UILabel! | @IBOutlet weak var helloLabel: UILabel! | ||
override func viewDidLoad() { | override func viewDidLoad() { | ||
Line 46: | Line 47: | ||
@IBAction func showAlertAction(_ sender: UIButton) { | @IBAction func showAlertAction(_ sender: UIButton) { | ||
let alertCtrl = UIAlertController(title: "Alert", message: "Hello Alert", preferredStyle: .alert) | let alertCtrl = UIAlertController(title: "Alert", message: "Hello Alert", preferredStyle: .alert) | ||
let okAction = UIAlertAction(title: "OK", style: . | let destructiveAction = UIAlertAction(title: "Delete", style: .destructive, handler: nil) | ||
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil) | |||
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil) | |||
alertCtrl.addAction(okAction) | alertCtrl.addAction(okAction) | ||
alertCtrl.addAction(cancelAction) | |||
alertCtrl.addAction(destructiveAction) | |||
present(alertCtrl, animated: true, completion: nil) | present(alertCtrl, animated: true, completion: nil) | ||
} | } | ||
Line 53: | Line 58: | ||
@IBAction func showConfirmAction(_ sender: UIButton) { | @IBAction func showConfirmAction(_ sender: UIButton) { | ||
let actionSheet = UIAlertController(title: "Hello World", message: "This is an Action Sheet", preferredStyle: .actionSheet) | let actionSheet = UIAlertController(title: "Hello World", message: "This is an Action Sheet", preferredStyle: .actionSheet) | ||
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) | |||
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) | let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) | ||
actionSheet.addAction(okAction) | actionSheet.addAction(okAction) | ||
actionSheet.addAction(cancelAction) | |||
present(actionSheet, animated: true, completion: nil) | present(actionSheet, animated: true, completion: nil) | ||
} | } |
Revision as of 00:19, 8 April 2019
export PATH=/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin:"${PATH}"
Hello World
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var nameTexField: UITextField!
@IBOutlet weak var ageTextField: UITextField!
@IBOutlet weak var helloLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func clickMeAction(_ sender: Any) {
print("Hello \(nameTexField.text!)");
helloLabel.text = "Hello \(nameTexField.text!), your age is \(ageTextField.text!)";
}
}
Alert & Action
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var nameTexField: UITextField!
@IBOutlet weak var ageTextField: UITextField!
@IBOutlet weak var helloLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func clickMeAction(_ sender: Any) {
print("Hello \(nameTexField.text!)")
helloLabel.text = "Hello \(nameTexField.text!), your age is \(ageTextField.text!)"
}
@IBAction func showAlertAction(_ sender: UIButton) {
let alertCtrl = UIAlertController(title: "Alert", message: "Hello Alert", preferredStyle: .alert)
let destructiveAction = UIAlertAction(title: "Delete", style: .destructive, handler: nil)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertCtrl.addAction(okAction)
alertCtrl.addAction(cancelAction)
alertCtrl.addAction(destructiveAction)
present(alertCtrl, animated: true, completion: nil)
}
@IBAction func showConfirmAction(_ sender: UIButton) {
let actionSheet = UIAlertController(title: "Hello World", message: "This is an Action Sheet", preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
actionSheet.addAction(okAction)
actionSheet.addAction(cancelAction)
present(actionSheet, animated: true, completion: nil)
}
}
Features
- Closures unified with function pointers
- Tuples and multiple return values
- Generics
- Fast and concise iteration over a range or collection
- Structs that support methods, extensions, and protocols
- Functional programming patterns, e.g., map and filter
- Powerful error handling built-in
- Advanced control flow with do, guard, defer, and repeat keywords