Swift开发UIAlertController使用详解

运行效果.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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,773评论 4 61
  • 看来我是易抑郁体制的孩子。在自己无比灰心丧气的时候无比渴望的,居然是好姐妹的拥抱或者陪伴。 阿盈去找阿杜玩,抱起我...
    AdaBleau阅读 1,229评论 0 1
  • 我有压岁钱。是来自老人的。你还有么? 每年过年的时候都会收到奶奶的压包钱,每个人都有份。大姐都二十八了,奶奶照给。...
    箩七阅读 2,675评论 0 1
  • 亲爱的家人和孩子们: 大家好!本周是是精彩的一周,是充满挑战的一周,也是小牵牛花们最开心的一周。只因我们走出...
    于向雨阅读 2,593评论 0 0
  • 穿越千年的眼泪,只有梦里看得见,窗台人影独坐 夜沉的更寂寞,一段路分两头 爱了却要放手,无事东风走过,扬起...
    喂喂那片海阅读 3,003评论 0 4

友情链接更多精彩内容