我有一個帶有4個UITextFields的ViewController。前3個由他們自己的UIPickerViews填充,第4個由UIDatePicker填充。在UIPickerView和UIDatePicker中選擇第一個或任何具有完成按鈕的行Swift
我想要一個完成按鈕能夠選擇每個選擇器的第一行,或者選擇無論哪一行被選中,如果不是第一行。
下面是適用於第一個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]
}
}
}
你想爲完成按鈕操作中的每個「UITextField」或「UIPickerView」分別執行代碼嗎? – user3589771
我試過這個的幾個變化,似乎無法弄清楚。 –