Swift - 警告提示框(UIAlertController)的用法

來源:互聯網
上載者:User

標籤:菜單   pre   des   present   swift   comment   and   save   ret   

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 為 ActionSheet        let 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)

Swift - 警告提示框(UIAlertController)的用法

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.