【问题标题】:getting Error While using Date picker class ActionSheetDatePicker.init使用日期选择器类 ActionSheetDatePicker.init 时出错
【发布时间】:2021-08-31 03:08:55
【问题描述】:

您好,我有一个要求,我需要在 uiactionsheet 中显示日期

我的 IBOutlet

@IBOutlet weak var dobTextField: UITextField!

点击日期字段时我在这里调用函数

if textField == dobTextField {
        self.view.endEditing(true)
        super.datePickerTapped { (dateString) in
            self.dobTextField.text = dateString
        }
        return false
    }

这是我在点击日期字段后调用的函数

func datePickerTapped(completionHandler: @escaping ((String)->Void)) {
        
        let cancelButton:UIButton =  UIButton(type: .custom)
        cancelButton.setTitle("Cancel", for: .normal)
        cancelButton.titleLabel?.font = crudFonts.appRegularFont
        cancelButton.setTitleColor(UIColor.black, for: .normal)
        cancelButton.frame = CGRect(x: 0, y: 0, width: 55, height: 25)
        
        let doneButton:UIButton =  UIButton(type: .custom)
        doneButton.setTitle("Done", for: .normal)
        doneButton.titleLabel?.font = crudFonts.appRegularFont
        doneButton.setTitleColor(UIColor.black, for: .normal)
        doneButton.frame = CGRect(x: 0, y: 0, width: 55, height: 25)
        
        let maxiumDate = Calendar.init(identifier: Calendar.Identifier.gregorian).date(byAdding: .year, value: -18, to: Date())
        let picker = ActionSheetDatePicker.init(title: "Date of Birth", datePickerMode: .date, selectedDate: maxiumDate, doneBlock: { (picker, selectedIndex, selectedValue) in
            
            if let date = selectedIndex as? Date {
                let formatter = DateFormatter()
                formatter.dateFormat = "dd/MM/YYYY"
                completionHandler(formatter.string(from: date))
            }
        }, cancel: { (picker) in
            
        }, origin: Global.getTopMostViewController()?.view)

        picker?.minimumDate = Calendar.init(identifier: Calendar.Identifier.gregorian).date(byAdding: .year, value: -100, to: Date())
        picker?.maximumDate = maxiumDate

        picker?.toolbarButtonsColor = .black
        picker?.toolbarBackgroundColor = UIColor.white
        picker?.setCancelButton(UIBarButtonItem(customView: cancelButton))
        picker?.setDoneButton(UIBarButtonItem(customView: doneButton))
        
        picker?.show()
        
        if let datePicker = picker?.pickerView as? UIDatePicker {
            if #available(iOS 13.4, *) {
                datePicker.preferredDatePickerStyle = UIDatePickerStyle.wheels
            }
        }
    }

错误:- 表达式类型不明确,没有更多上下文

在这一行:- let picker = ActionSheetDatePicker.init(title: "Date of Birth", datePickerMode: .date, selectedDate: maxiumDate, doneBlock: { (picker, selectedIndex, selectedValue) in

【问题讨论】:

    标签: ios swift datepicker


    【解决方案1】:

    看起来像以下两个问题之一 -

    // This returns an optional date `Date?`
    let maxiumDate = Calendar.init(identifier: Calendar.Identifier.gregorian).date(byAdding: .year, value: -18, to: Date())
    
    // 1. Check whether `selectedDate: maxiumDate` accept a `Date?` or `Date` instance.
    
    // 2. Check the number of arguments in `(picker, selectedIndex, selectedValue) in` part
    // and what the docs say
    let picker = ActionSheetDatePicker.init(title: "Date of Birth", datePickerMode: .date, selectedDate: maxiumDate, doneBlock: { (picker, selectedIndex, selectedValue) in
    

    【讨论】:

    • 我必须在 ActionSheetDatePicker 初始化程序中传递更多参数,但我没有传递目标、操作和原点的内容
    • 阅读该库的文档,然后传递您需要的所有内容。
    • 它是一个完整的函数:- let pickers = ActionSheetDatePicker.init(title: , datePickerMode: , selectedDate: ,目标:,动作:,原点:)
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 2023-03-16
    • 2016-02-16
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多