Swift开发UIAlertController使用详解

Swift开发UIAlertController使用详解_第1张图片
运行效果.gif

上面分享的文章中有一小部分代码随着Swift版本的更新失效了,不过大部分还是非常好用的,下面将本人根据上面的文章写得实际使用UIAlertController的代码展示给大家,本文只
提供Swift版本的使用方式,有使用OC的课移步 参考文章

话不多说,上代码。

// MARK: - 默认样式,注意:Swift中必须声明是iOS8.0以上才执行UIAlertController相关的代码,否则会报错
if #available(iOS 8.0, *) 
{
      let alertController = UIAlertController(title: "提示", message: "这个是UIAlertController的默认样式", preferredStyle: UIAlertControllerStyle.Alert)
      let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
      let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default, handler: nil)
      alertController.addAction(cancelAction)
      alertController.addAction(okAction)
      self.presentViewController(alertController, animated: true, completion: nil)
                
 } else {
              // Fallback on earlier versions  
 }


  // MARK: -  警示样式
if #available(iOS 8.0, *) {
      let alertController = UIAlertController(title: "订单", message: "这个是UIAlertController的警示样式", preferredStyle: UIAlertControllerStyle.Alert)
      let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
      let okAction = UIAlertAction(title: "警示", style: UIAlertActionStyle.Destructive, handler: nil)
      alertController.addAction(cancelAction)
      alertController.addAction(okAction)
      self.presentViewController(alertController, animated: true, completion: nil)
                
 } else {
      // Fallback on earlier versions
 }


   // MARK: - 文本对话框
    if #available(iOS 8.0, *) {
 
    let alertController = UIAlertController(title: "优惠", message: "这个是UIAlertController的文本对话框", preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addTextFieldWithConfigurationHandler({ (textField: UITextField!) -> Void in
    textField.placeholder = "登录"
    
    // 添加监听代码,监听文本框变化时要做的操作
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("alertTextFieldDidChange:"), name: UITextFieldTextDidChangeNotification, object: textField)
    })
    
    alertController.addTextFieldWithConfigurationHandler({ (textField: UITextField!) -> Void in
    textField.placeholder = "密码"
    textField.secureTextEntry = true
    })
    
    let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
    let okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default , handler: { (action: UIAlertAction!) -> Void in
    let login = (alertController.textFields?.first)! as UITextField
    let password = (alertController.textFields?.last)! as UITextField
    print("loginStr===\(login.text),passWordStr===\(password.text)")
    
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextFieldTextDidChangeNotification, object: nil)
    
    })
    okAction.enabled = false
    alertController.addAction(cancelAction)
    alertController.addAction(okAction)
    self.presentViewController(alertController, animated: true, completion: nil)
    
    } else {
    // Fallback on earlier versions
    }

// MARK: - 用户名长度超过3个字符才可以点击“好的”按钮,好了,现在对话框的“好的”按钮被冻结了,除非在“登录”文本框中输入3个以上的字符:
    func alertTextFieldDidChange(notification: NSNotification){
        if #available(iOS 8.0, *) {
            let alertController = self.presentedViewController as! UIAlertController?
            if (alertController != nil) {
                let login = (alertController!.textFields?.first)! as UITextField
                let okAction = alertController!.actions.last! as UIAlertAction
                
                if login.text?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 3 {
                    okAction.enabled = true
                } else {
                    okAction.enabled = false
                }
            }
            
        } else {
            // Fallback on earlier versions
        }
        
    }
// MARK:  - 上拉菜单
            if #available(iOS 8.0, *) {
                let alertController = UIAlertController(title: "保存或删除数据", message: "删除数据将不可恢复", preferredStyle: UIAlertControllerStyle.ActionSheet)
                // 如果上拉菜单中有“取消”按钮的话,那么它永远都会出现在菜单的底部,不管添加的次序是如何(就是这么任性)。其他的按钮将会按照添加的次序从上往下依次显示
                let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
                let deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: nil)
                let archiveAction = UIAlertAction(title: "保存", style: UIAlertActionStyle.Default, handler: nil)
                alertController.addAction(cancelAction)
                alertController.addAction(deleteAction)
                alertController.addAction(archiveAction)
                self.presentViewController(alertController, animated: true, completion: nil)
                
            } else {
                // Fallback on earlier versions
            }

这是小编最近在写的下厨房Swift高仿版的一部分,有兴趣的可移步GitHub

你可能感兴趣的:(Swift开发UIAlertController使用详解)