如何在viewForRow上的UIPickerView中添加UITextField

时间:2017-04-10 21:44:28

标签: ios swift uitextfield uipickerview

我在UIPickerView的viewForRow事件中尝试了以下代码。标签出现时不显示文本字段。是否允许在选择器视图中添加文本字段?

这是我的代码:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    var rowDict = NSDictionary()
    var retView = UIView()

    let label = UILabel(frame: CGRect.init(x: 0, y: 0, width: 400, height: 40) )
    let textField = UITextField(frame: CGRect.init(x: 0, y: 0, width: 400, height: 40) )

    if(pickerView == pickerView2)
    {
        rowDict = self.arrayList02[row] as! NSDictionary
        let rowValue = rowDict["QueryAttribute"] as? String
        label.numberOfLines = 0
        label.text = rowValue
        label.sizeToFit()
        if rowValue == "Other Specify:"{
            textField.placeholderText = rowValue
            textField.isUserInteractionEnabled = true
            retView = textField
        }
        else {
            retView = label
        }
    }

    return retView
}

1 个答案:

答案 0 :(得分:0)

  

我尝试使用此代码来选择UIPickerView中的特定选项的用户输入

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        selectionHandler?(pickerView, row, arrayList[row] as! String)
        var rowValue = NSDictionary()
        if(arrayList.count > 0) {
            rowValue = self.arrayList[row] as! NSDictionary
            if(rowValue["ItemCode"] as? String != "99"){
                textView.text = rowValue["Item"] as? String
                textView.tag = rowValue["Id"] as! Int
            }

            if(isOtherSpecifyRequired){
                let lastRow = pickerView.numberOfRows(inComponent: component) - 1
                let lastRowValue = rowValue["Item"] as? String
                setOtherSpecifyField(lastRow: lastRow, row: row, lastRowValue: lastRowValue!, textField: textView)
            }
        }
    }

    func setOtherSpecifyField(lastRow: Int, row: Int, lastRowValue: String, textField: CustomTextField){
        if row == lastRow{
            ShowAlertWithTextBox(title: "Other Specify", message: "Please enter some text ", parentView: textField)
            textField.tag = 0
        }
    }

    func ShowAlertWithTextBox(title: String, message: String, parentView: UITextField)
    {
        let alert = UIAlertController(title: title , message: message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addTextField(configurationHandler: textFieldHandler)
        alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.default, handler: nil))
        alert.addAction(UIAlertAction(title: "Submit", style: UIAlertActionStyle.default, handler: { (UIAlertAction) in
            parentView.text = alert.textFields?[0].text }))
        if let presented = _viewController.presentedViewController {
            presented.removeFromParentViewController()
        }
        if _viewController.presentedViewController == nil {
            self._viewController.present(alert, animated: true, completion: nil)
        }
    }

    func textFieldHandler(textField: UITextField!)
    {
        if (textField) != nil {
            textField.placeholder = "Other Specify"
        }
    }