2017-06-01 176 views
2

我在下面的文本字段實現UIPickerView如何在單個頁面中實現多個UIPickerView?

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

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
    return states.count // states is an array of strings 
} 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
    return states[row] 
} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
    textField.text = states[row] 
    self.view.endEditing(false) 
} 

我想要實現UIPickerView另一個文本框。我應該怎麼做?我可以改變上面實施的方法來包含兩個文本字段嗎?

回答

4

輕鬆,你可以設置每個pickerView標籤值,例如:

let firstPV = UIPickerView() 
pickerV.tag = 1 

let secondPV = UIPickerView() 
secondPV.tag = 2 

這樣,只是你在PickerView」協議的標籤屬性來評估,如:

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

    switch pickerView.tag { 
     case 1: 
      return 1 
     case 2: 
      return 3 
    default: 
     return 0 
    } 
} 
相關問題