美文网首页
点击文本框(Label)弹出datePicker_Swift

点击文本框(Label)弹出datePicker_Swift

作者: YHWXQ简简单单的生活 | 来源:发表于2016-08-30 19:32 被阅读492次

    最近,在项目中实现了一个点击文本框弹出datePicker的效果,废话不多说,先看效果图:

    Paste_Image.png
    问题:我在开发的过程中,遇到了一个问题,点击文本框会弹出键盘?
    解决方法 1:

    代码:

    import UIKit
    
    class ViewController: UIViewController, UITextFieldDelegate {
    
        @IBOutlet weak var birthdayTextField: UITextField!
        // 创建一个日期格式器
        let dformatter = NSDateFormatter()
        override func viewDidLoad() {
            super.viewDidLoad()
           birthdayTextField.delegate = self
        }
    
        func alert() {
            //将日期选择器区域设置为中文,则选择器日期显示为中文
            let birthdayDatePicker = UIDatePicker(frame: CGRectMake(0, 40, 320, 200))
            birthdayDatePicker.locale = NSLocale(localeIdentifier: "zh_CN")
            // 设置样式,当前设为同时显示日期和时间
            birthdayDatePicker.datePickerMode = UIDatePickerMode.Date
            // 响应事件(只要滚轮变化就会触发)
            birthdayDatePicker.addTarget(self, action:nil, forControlEvents: UIControlEvents.ValueChanged)
            
            // 为日期格式器设置格式字符串
            dformatter.dateFormat = "yyyy-MM-dd"
            // 设置初始日期为2000-01-01da
            birthdayDatePicker.date = dformatter.dateFromString("2000-01-01")!
            birthdayDatePicker.maximumDate = NSDate()
            
            //  初始化alertController
            let alertController = UIAlertController(title: "请选择\n\n\n\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle:  .ActionSheet)
            let cancleAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
            let doneAction = UIAlertAction(title: "确定", style: .Default) { (action: UIAlertAction) in
                // 使用日期格式器格式化日期、时间
                let date = birthdayDatePicker.date
                let birthdate = self.dformatter.stringFromDate(date)
                self.birthdayTextField.text = birthdate
                self.view.endEditing(true)
            }
            alertController.view.addSubview(birthdayDatePicker)
            alertController.addAction(doneAction)
            alertController.addAction(cancleAction)
            self.presentViewController(alertController, animated: true, completion: nil)  
        }
        
        // 防止点击文本框弹出键盘
        func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
            textField.resignFirstResponder()
            alert()
            return false
        }
    }
    
    解决方法 2:通过添加手势,点击Label弹出datePicker的效果

    代码:

    import UIKit
    
    class ViewController: UIViewController {
       
        @IBOutlet weak var birthdayLabel: UILabel!
        // 创建一个日期格式器
        let dformatter = NSDateFormatter()
        override func viewDidLoad() {
            super.viewDidLoad()     
            // 开启用户交互
            birthdayLabel.userInteractionEnabled = true
            //点击事件
            let tap = UITapGestureRecognizer(target: self, action: #selector(self.alertDatePicker))
            //绑定tap
            birthdayLabel.addGestureRecognizer(tap)
        }
    
        func alertDatePicker() {
            alert()
        }
        
        func alert() {
            //将日期选择器区域设置为中文,则选择器日期显示为中文
            let birthdayDatePicker = UIDatePicker(frame: CGRectMake(0, 40, 320, 200))
            birthdayDatePicker.locale = NSLocale(localeIdentifier: "zh_CN")
            // 设置样式,当前设为同时显示日期和时间
            birthdayDatePicker.datePickerMode = UIDatePickerMode.Date
            // 响应事件(只要滚轮变化就会触发)
            birthdayDatePicker.addTarget(self, action:nil, forControlEvents: UIControlEvents.ValueChanged)
            
            // 为日期格式器设置格式字符串
            dformatter.dateFormat = "yyyy-MM-dd"
            // 设置初始日期为2000-01-01da
            birthdayDatePicker.date = dformatter.dateFromString("2000-01-01")!
            birthdayDatePicker.maximumDate = NSDate()
            
            //  初始化alertController
            let alertController = UIAlertController(title: "请选择\n\n\n\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle:  .ActionSheet)
            let cancleAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
            let doneAction = UIAlertAction(title: "确定", style: .Default) { (action: UIAlertAction) in
                // 使用日期格式器格式化日期、时间
                let date = birthdayDatePicker.date
                let birthdate = self.dformatter.stringFromDate(date)
                self.birthdayLabel.text = birthdate
                self.view.endEditing(true)
            }
            alertController.view.addSubview(birthdayDatePicker)
            alertController.addAction(doneAction)
            alertController.addAction(cancleAction)
            self.presentViewController(alertController, animated: true, completion: nil)
        }
    }
    

    相关文章

      网友评论

          本文标题:点击文本框(Label)弹出datePicker_Swift

          本文链接:https://www.haomeiwen.com/subject/zzfqettx.html