2017-08-11 53 views
1

我有一個帶有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] 
    } 
} 
} 
+0

你想爲完成按鈕操作中的每個「UITextField」或「UIPickerView」分別執行代碼嗎? – user3589771

+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

+0

我更改了代碼,但是當我使用完成按鈕選擇第一個選擇器中的第一行時,它也選擇了第二個選擇器中的第一行。 –

+0

是的,因爲你問。 「我想要一個完成按鈕能夠選擇每個選擇器的第一行,或者選擇不選第一行的那一行。」我已經以這種方式編寫了代碼,你想要在DoneButton Action上輸出什麼?請告訴我,我會彌補您的要求 –

+0

謝謝@iOS Geek。你是代碼工程,但它同時選擇了每個選擇器的第一行。我應該對我的問題更具體一些。我想要的是完成按鈕選取器工具欄只能用於該選取器。我可能需要第一個選擇器中的第一行,但第二個選擇器中的第三行。我在下面粘貼的代碼允許我在選擇器中選擇任何可見的行而不影響其他3個拾取器。這段代碼看起來好嗎還是有更好的方法來編寫它? –

0

所以我想出答案對我自己的問題。我試圖爲@ user3589771建議的每個UITextFiled和UIPickerView創建單獨的代碼,但我錯過了一些東西。

我原來的代碼有

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

但一旦我分離.inputAccessoryView每個的UITextField都是走到了一起。

我終於整理出來了,答案如下。

func createToolbar() { 

    // Tool bar for 1st UIPickerView 

    let toolBar1 = UIToolbar() 
     toolBar1.sizeToFit() 
     toolBar1.barTintColor = .black 
     toolBar1.tintColor = .white 

    let doneButton1 = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(RunInputViewController.doneButton1(sender:))) 
     toolBar1.setItems([doneButton1], animated: false) 
     toolBar1.isUserInteractionEnabled = true 
     runTypeTextField.inputAccessoryView = toolBar1 

    // Tool bar for 2nd UIPickerView 

    let toolBar2 = UIToolbar() 
     toolBar2.sizeToFit() 
     toolBar2.barTintColor = .black 
     toolBar2.tintColor = .white 

    let doneButton2 = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(RunInputViewController.doneButton2(sender:))) 
     toolBar2.setItems([doneButton2], animated: false) 
     toolBar2.isUserInteractionEnabled = true 
     runTextField.inputAccessoryView = toolBar2 

    // Tool bar for 3rd UIPickerView 


    let toolBar3 = UIToolbar() 
     toolBar3.sizeToFit() 
     toolBar3.barTintColor = .black 
     toolBar3.tintColor = .white 

    let doneButton3 = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(RunInputViewController.doneButton3(sender:))) 
     toolBar3.setItems([doneButton3], animated: false) 
     toolBar3.isUserInteractionEnabled = true 
     snowTypeTextField.inputAccessoryView = toolBar3 

    // Tool bar for UIDatePicker 

    let toolBar4 = UIToolbar() 
     toolBar4.sizeToFit() 
     toolBar4.barTintColor = .black 
     toolBar4.tintColor = .white 

    let doneButton4 = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(RunInputViewController.doneButton4)) 
     toolBar4.setItems([doneButton4], animated: false) 
     toolBar4.isUserInteractionEnabled = true 
     dateTextField.inputAccessoryView = toolBar4 

} 

// Done button for 1st UIPickerView 


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

// Done button for 2nd UIPickerView 

func doneButton2(sender: AnyObject) { 
    let row2 = pickerView2.selectedRow(inComponent: 0); 
     pickerView(pickerView2, didSelectRow: row2, inComponent:0) 
     view.endEditing(true) 
} 
// Done button for 3rd UIPickerView 

func doneButton3(sender: AnyObject) { 
    let row3 = pickerView3.selectedRow(inComponent: 0); 
     pickerView(pickerView3, didSelectRow: row3, inComponent:0) 
     view.endEditing(true) 
} 

// Done button for UIDatePicker 

func doneButton4(){ 
    let formatter = DateFormatter() 
     formatter.dateFormat = "dd/MM/yyyy HH:mm" 
     dateTextField.text = formatter.string(from: pickerView4.date) 
     self.view.endEditing(true) 
} 
相關問題