选择器没有选择值

时间:2018-04-19 03:03:34

标签: arrays swift uipickerview textfield

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

选择器会弹出并正常工作。 3个变量是:"小时" ,"减半"和"键入"。

所有帮助赞赏

var timePickerData = [[" 4"," 5"," 6"," 7"," 8&# 34;," 9"" 10"" 11"" 12"" 13"&# 34; 14"" 15"" 16"],[" 0"" 15"" 30& #34;," 45"],["小时"," Bins"]]

//设置timePicker   //要在

中传递的行和组件(列)返回的数据
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 个答案:

答案 0 :(得分:0)

您应该在setTextfield中调用didSelectRow方法,如下所示

// 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()
    }