在UIPickerView和UIDatePicker for Swift中选择第一行或任何带有完成按钮的行

时间:2017-08-11 06:10:35

标签: swift uipickerview uidatepicker

我有一个带有4个UITextField的ViewController。前3个由他们自己的UIPickerViews填充,第4个由UIDatePicker填充。

我想要一个完成按钮,以便能够选择每个选择器的第一行,或者选择哪一行(如果不是第一行)。

以下是适用于第一个UIPicker的代码。从Selecting first row in UIPickerView issue得到它。

func doneButton(sender: AnyObject) {
    let row = pickerView1.selectedRow(inComponent: 0);
    pickerView(pickerView1, didSelectRow: row, inComponent:0)
    view.endEditing(true)
}

如何让完成按钮为所有4个选择器独立工作?

这是完整的代码。

任何帮助都非常感激。

var selectedItemsArray = [String]()

class RunInputViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate{

var pickerView1 = UIPickerView()
var pickerView2 = UIPickerView()
var pickerView3 = UIPickerView()
var pickerView4 = UIDatePicker()

@IBOutlet weak var runTypeTextField: UITextField!
@IBOutlet var runTextField: UITextField!
@IBOutlet var snowTypeTextField: UITextField!
@IBOutlet var dateTextField: UITextField!


@IBAction func saveRun(_ sender: Any){
    if (runTypeTextField.text != ""){
    runFeedList.append(runTypeTextField.text!)
        runTypeTextField.text = ""
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    createToolbar()

    pickerView1 = UIPickerView()
    pickerView2 = UIPickerView()
    pickerView3 = UIPickerView()
    pickerView4 = UIDatePicker()

    pickerView1.delegate = self
    pickerView1.dataSource = self

    pickerView2.delegate = self
    pickerView2.dataSource = self

    pickerView3.delegate = self
    pickerView3.dataSource = self


    pickerView1.tag = 0
    pickerView2.tag = 1
    pickerView3.tag = 2
    pickerView4.tag = 3

    self.runTypeTextField.inputView = self.pickerView1;
    self.runTextField.inputView = self.pickerView2;
    self.snowTypeTextField.inputView = self.pickerView3;
    self.dateTextField.inputView = self.pickerView4;

    pickerView4.addTarget(self, action: #selector(datePickerValueChanged(sender:)), for: .valueChanged)

    pickerView4.datePickerMode = UIDatePickerMode.time
    //let currentDate = NSDate()
    //pickerView4.minimumDate = currentDate as Date
    //pickerView4.date = currentDate as Date

    pickerView4.timeZone = NSTimeZone.local
    pickerView4.minuteInterval = 5
}



func createToolbar() {

    let toolBar = UIToolbar()
    toolBar.sizeToFit()

    //Customizations
    toolBar.barTintColor = .black
    toolBar.tintColor = .white

    let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(RunInputViewController.doneButton(sender:)))

    toolBar.setItems([doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    runTypeTextField.inputAccessoryView = toolBar
    runTextField.inputAccessoryView = toolBar
    snowTypeTextField.inputAccessoryView = toolBar
    dateTextField.inputAccessoryView = toolBar
}

func doneButton(sender: AnyObject) {

    let row = pickerView1.selectedRow(inComponent: 0);
    pickerView(pickerView1, didSelectRow: row, inComponent:0)
    view.endEditing(true)
}

func dismissKeyboard() {
    view.endEditing(true)
}

func datePickerValueChanged(sender:UIDatePicker) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "HH:mm"
    pickerView4.datePickerMode = UIDatePickerMode.time
    dateTextField.text = dateFormatter.string(from: sender.date)
}


func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

    if pickerView.tag == 0 {
        return runType.count
    } else if pickerView.tag == 1 {
        return selectedItemsArray.count
    } else if pickerView.tag == 2 {
        return snowType.count
    }
    return 1

}

 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

if pickerView.tag == 0 {
    return runType[row]
} else if pickerView.tag == 1 {
    return selectedItemsArray[row]
} else if pickerView.tag == 2 {
    return snowType[row]
}

return ""

}


var selectedRow: Int = 0

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {

    selectedRow = row

    if pickerView == pickerView1 {
        runTypeTextField.text = runType[row]
        switch row {
        case 0:
            selectedItemsArray = RunGreen
        case 1:
            selectedItemsArray = RunBlue
        case 2:
            selectedItemsArray = RunBlack
        case 3:
            selectedItemsArray = RunDoubleBlack

        default:
            selectedItemsArray = []
        }
        pickerView2.reloadAllComponents()

    } else if pickerView == pickerView2 {
        let item = selectedItemsArray[row]
        if selectedItemsArray == rRunGreen {
            runTextField.text = item
        } else if selectedItemsArray == RunBlue {
            runTextField.text = item
        } else if selectedItemsArray == rRunBlack {
            runTextField.text = item
        } else if selectedItemsArray == RunDoubleBlack {
            runTextField.text = item
        }
    }
        if pickerView.tag == 2 {
        snowTypeTextField.text = snowType[row]
    }
}
}

1 个答案:

答案 0 :(得分:0)

检查此代码是否使用具有不同数组输入和一个日期选择器的两个选择器

import UIKit

class PickerViewController: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate {
    @IBOutlet weak var pickerVieww: UIPickerView!

    @IBOutlet weak var DateePicker: UIDatePicker!
    @IBOutlet weak var newPickr: UIPickerView!
    var viewControllerArray = [String]()
    var viewControllerArray1 = [String]()
    var value = Int()
    var value1 = Int()

    override func viewDidLoad() {
        super.viewDidLoad()
        pickerVieww.delegate = self
        pickerVieww.dataSource = self
        newPickr.delegate = self
        newPickr.dataSource = self
        pickerVieww.tag = 1
        newPickr.tag = 2
        viewControllerArray = ["controller1","controller2","controller3"]
        viewControllerArray1 = ["controller4","controller5","controller6","controller7"]
    }
    func numberOfComponents(in pickerView: UIPickerView) -> Int {

        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView == pickerVieww
        {
            return viewControllerArray.count
        }
        else if pickerView == newPickr
        {
            return viewControllerArray1.count
        }
        return 0
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView == pickerVieww
        {
            return viewControllerArray[row]
        }
        else if pickerView == newPickr
        {
            return viewControllerArray1[row]
        }

        return ""
    }
    func pickerView(_ pickerView: UIPickerView,didSelectRow row: Int,inComponent component: Int)
    {


    }
    @IBAction func DoneButton(_ sender: Any) {

        let row = pickerVieww.selectedRow(inComponent: 0);
       print("pickervieww value: \(row)")

        let row1 = newPickr.selectedRow(inComponent: 0);
        print("newpicker value: \(row1)")

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd MMM yyyy"
        let date:String = dateFormatter.string(from: DateePicker.date)
        print("date: \(date)")

    }

}

按钮上的控制台输出操作

enter image description here