Swift - 警告提示框(UIAlertController)的用法
生活随笔
收集整理的這篇文章主要介紹了
Swift - 警告提示框(UIAlertController)的用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import UIKitclass ViewController: UIViewController {override func viewDidLoad() {super.viewDidLoad()}override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {// 創建let alertController = UIAlertController(title: "提示", message: "你確定要離開?", preferredStyle:.Alert) // 設置2個UIAlertAction let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil) let okAction = UIAlertAction(title: "好的", style: .Default) { (UIAlertAction) in print("點擊了好的") } // 添加 alertController.addAction(cancelAction) alertController.addAction(okAction) // 彈出 self.presentViewController(alertController, animated: true, completion: nil) } } // 除了彈出,還可以使用底部向上滑出的樣式// 注意:如果上拉菜單中有『取消』按鈕的話,那么它永遠都會出現在菜單的底部,不管添加的次序如何// 創建// preferredStyle 為 ActionSheetlet alertController = UIAlertController(title: "保存或刪除數據", message: "刪除數據將不可恢復", preferredStyle:.ActionSheet) // 設置2個UIAlertAction let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil) let deleteAction = UIAlertAction(title: "刪除", style: .Destructive, handler: nil) let saveAction = UIAlertAction(title: "保存", style: .Default, handler: nil) // 添加到UIAlertController alertController.addAction(cancelAction) alertController.addAction(saveAction) alertController.addAction(deleteAction) // 彈出 self.presentViewController(alertController, animated: true, completion: nil) /*添加任意數量的文本輸入框(比如可以用來實現登錄框)*/let alertController = UIAlertController(title: "系統登錄", message: "請輸入用戶名和密碼", preferredStyle: UIAlertControllerStyle.Alert)alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) in textField.placeholder = "用戶名" } alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) in textField.placeholder = "密碼" textField.secureTextEntry = true } let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil) let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default) { (UIAlertAction) in let login = alertController.textFields![0] let pwd = alertController.textFields![1] print("用戶名:\(login.text) 密碼:\(pwd.text)") } alertController.addAction(cancelAction) alertController.addAction(okAction) // 彈出 self.presentViewController(alertController, animated: true, completion: nil)
轉載于:https://www.cnblogs.com/Free-Thinker/p/6372952.html
總結
以上是生活随笔為你收集整理的Swift - 警告提示框(UIAlertController)的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 简单几行代码实现摇一摇功
- 下一篇: Android 设置view透明度,广告