选择器不选择值

Posted

技术标签:

【中文标题】选择器不选择值【英文标题】:picker not selecting values 【发布时间】:2018-04-19 03:03:34 【问题描述】:

大家好,我制作了一个自定义选择器视图,让用户从 3 行而不是键盘输入中选择。问题是选择器没有将选定的值放入文本字​​段。

选择器确实弹出并且工作正常。这 3 个变量是:“小时”、“减半”和“类型”。

感谢所有帮助

var timePickerData = [["4","5","6","7","8","9","10","11","12","13","14 ","15","16"],["0","15","30","45"],["Hours","Bins"]]

// 设置时间选择器。 // 要为传入的行和组件(列)返回的数据

func numberOfComponents(in pickerView: UIPickerView) -> Int  return timePickerData.count


// The number of rows of data
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    return timePickerData[component].count


// Get title for Row
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
    return timePickerData[component][row]


// Capture the picker view selection
func pickerView(_pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
    if row == 1 
        hours = String(describing: timePickerData[component])
    
    if row == 2 
        halve = String(describing: timePickerData[component])
    
    if row == 3 
        type = String(describing: timePickerData[component])
    

    calculateMoney()


func setTextfield() 
    timeFilledIn.text = hours + "." + halve + " " + type



func calculateMoney()

    let hoursStr = self.hours
    let hours = (hoursStr as NSString).doubleValue
    let halveStr = self.halve
    let halve = (halveStr as NSString).doubleValue
    let payHours = (payFilledIn.text! as NSString).doubleValue
    let totalMinutes = 60.00
    let tax = 0.85

    let payMinutes = ( payHours/totalMinutes )
    let calculatedHours = ( hours * payHours )
    let calculatedMinutes = (halve * payMinutes)
    let calculatedTotal = (calculatedHours + calculatedMinutes * tax)

    numberFormatter.numberStyle = .currency
    numberFormatter.locale = Locale(identifier: "es_CL")

    moneyMade.text = numberFormatter.string(from: NSNumber(value: calculatedTotal))


@objc func doneClick() 
    timeFilledIn.resignFirstResponder()
    calculateMoney()
    setTextfield()

@objc func cancelClick() 
    timeFilledIn.resignFirstResponder()
    timeFilledIn.text = "4" + "." + "0" + "    " + "Hours"
    calculateMoney()


// viewDidLoad
override func viewDidLoad()

    super.viewDidLoad()
    tappedAroundcustom()
    payFilledIn.delegate = self
    farmNameTxt.delegate = self
    timeFilledIn.delegate = self
    calculateMoney()
    timePickerData = [["4","5","6","7","8","9","10","11","12","13","14","15","16"],[ "0","15","30","45" ],["Hours","Bins"]]


    // set up toolbar for picker
    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor.black
    toolBar.sizeToFit()
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(doneClick))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancelClick))
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width / 3, height: self.view.frame.size.height))
    label.font = UIFont(name: "Gill Sans", size: 12)
    label.backgroundColor = UIColor.clear
    label.textColor = UIColor.black
    label.text = "Select work done today"
    label.textAlignment = NSTextAlignment.center
    let textBtn = UIBarButtonItem(customView: label)
    toolBar.setItems([cancelButton, spaceButton, textBtn , spaceButton , doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    // set up picker
    let timePicker = UIPickerView()
    timePicker.delegate = self
    timePicker.dataSource = self
    timePicker.backgroundColor = UIColor.lightGray
    timePicker.tintColor = UIColor.black
    timeFilledIn.inputView = timePicker
    timeFilledIn.inputAccessoryView = toolBar

【问题讨论】:

【参考方案1】:

你应该像下面这样在didSelectRow中调用setTextfield方法,

// Capture the picker view selection
    func pickerView(_pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        if row == 1 
            hours = String(describing: timePickerData[component])
        
        if row == 2 
            halve = String(describing: timePickerData[component])
        
        if row == 3 
            type = String(describing: timePickerData[component])
        

        calculateMoney()
        setTextfield()
    

【讨论】:

当您将选择器中的行更改为 setTextfield 并更新小时、减半和键入值时,您是否进行了调试? 不,我不可能这就是为什么它没有显示? @Stijnk008 是的,首先检查为什么您没有在didSelectRow 中获得行选择事件。 我修复了我在另一个pickerView函数thanx中犯了一个错误

以上是关于选择器不选择值的主要内容,如果未能解决你的问题,请参考以下文章

为啥这个引导日期选择器不显示日期选择器?

类的jquery选择器选择器不起作用(bootpeg)[重复]

SwiftUI 列表正在工作,但选择器不起作用

多个日期选择器不起作用

jQuery 选择器,包括属性和同级选择器不起作用

选定的选择器不起作用