ios - 从单个UIPicker获取两个标签的值-LMLPHP尝试使用单个UIPicker填充两个标签。它有一列,两个标签,每个标签的末尾都有两个按钮。单击后将显示UIPicker视图

@IBOutlet weak var userLoc1: UILabel!
@IBOutlet weak var userLoc2: UILabel!

let location = ["location1", "Location2", "Location3",
"Location4", "Location5", "Location6", "Location7"]


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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    var countRow:Int = 0

    if pickerView == LocationPickerView{
    countRow = self.location.count
    }
    return countRow
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == LocationPickerView
    {
    let titleLocation = location[row]
        return titleLocation
    }
    return ""
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == LocationPickerView
    {
        selectedLocation1 = self.location[row]
        self.userLoc1.text = self.location[row]
        self.LocationPickerView.isHidden = true

 //anything that can be done here that can assign different values to
  //two label  using the same UIPicker

    }


任何解决此问题的指南将不胜感激

最佳答案

只需在选择器视图中添加一个额外的列,然后添加第二个数组以填充该新列。

这与初始化pickerview所使用的方法相同,只是增加了一些逻辑。

let location1 = ["location1", "Location2", "Location3",
"Location4", "Location5", "Location6", "Location7"]

let location2 = ["location8", "Location9", "Location10",
"Location11", "Location12", "Location13", "Location14"]

// Set number of columns in your picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
   return 2
}

// Set number of rows of each column to length of arrays
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
   switch component {
   case 0:
      return self.location1
   case 1:
      return self.location2
   default: break
   }
}

// Set content of rows in each column
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
   switch component {
   case 0:
      return self.location1[row]
   case 1:
      return self.location2[row]
   default: break
   }
}


func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == LocationPickerView
{
   switch component {
   case 0:
      selectedLocation1 = self.location1[row]
      self.userLoc1.text = self.location1[row]
   case 1:
      selectedLocation2 = self.location2[row]
      self.userLoc2.text = self.location2[row]
   default: break
   }

   self.LocationPickerView.isHidden = true

}

关于ios - 从单个UIPicker获取两个标签的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46475541/

10-12 00:32