swift中UIAlertView的使用

前端之家收集整理的这篇文章主要介绍了swift中UIAlertView的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
// 方法1
let alertView = UIAlertView(title: alertTitle,message: alertMessage,delegate: nil,cancelButtonTitle: alertCancel)
alertView.show()
// 方法2
// 实例化时添加代理对象(注意添加协议)
let alertView = UIAlertView(title: alertTitle,delegate: self,cancelButtonTitle: alertCancel,otherButtonTitles: alertOK,"提示","通告","警告")
alertView.show()
// 添加协议 UIAlertViewDelegate
class ViewController: UIViewController,UIAlertViewDelegate {
    
    override func viewDidLoad() {
    ...
   }
   ...
}
// 实现协议方法
// MARK: UIAlertViewDelegate
func alertView(alertView: UIAlertView,clickedButtonAtIndex buttonIndex: Int) {
        let buttonTitle = alertView.buttonTitleAtIndex(buttonIndex)
        if buttonTitle == alertCancel
        {
            print("你点击了取消")
        }
        else if buttonTitle == alertOK
        {
            print("你点击了确定")
        }
        else
        {
            print("你点击了其他")
        }
}
// 方法3
// 1 实例化
let alertVC = UIAlertController(title: alertTitle,preferredStyle: UIAlertControllerStyle.Alert)
// 2 带输入框
alertVC.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "用户名"
}
alertVC.addTextFieldWithConfigurationHandler {
            (textField: UITextField!) -> Void in
            textField.placeholder = "密码"
            textField.secureTextEntry = true
}
// 3 命令(样式:退出Cancel,警告Destructive-按钮标题为红色,默认Default)
let alertActionCancel = UIAlertAction(title: alertCancel,style: UIAlertActionStyle.Destructive,handler: nil)
let alertActionOK = UIAlertAction(title: alertOK,style: UIAlertActionStyle.Default,handler: {
            action in
            print("OK")
            
            // 3-1 获取输入框的输入信息
            let username = alertVC.textFields!.first! as UITextField
            let password = alertVC.textFields!.last! as UITextField
            print("用户名:\(username.text),密码:\(password.text)")
})
alertVC.addAction(alertActionCancel)
alertVC.addAction(alertActionOK)
// 4 跳转显示
self.presentViewController(alertVC,animated: true,completion: nil)

方法1示例图


方法2示例图


方法3示例图

猜你在找的Swift相关文章