ios - 从单个 UIPicker 获取两个标签的值

标签 ios swift uipickerview

enter image description here尝试使用单个 UIPicker 填充两个标签。它有一列,两个标签,每个标签末尾有两个按钮。单击显示 UIPicker View

@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

    }

非常感谢任何解决问题的指南

最佳答案

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

这与初始化选取器 View 的方法相同,只是添加了一些逻辑。

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/

相关文章:

iphone - IOS Dev Library 在你的 mac 上离线

ios - 使 UIPickerView 依赖于选定的行(Swift)

ios - 3 个文本字段,1 个 pickerview,但是,使用 pickerView 时,选择内容不会显示在文本字段中

arrays - 在 Swift 中从 csv 文件读取数据并将数据转换为多维数组

ios - 带有动态变量的 NSLocalizedString (Swift) - 不工作

swift - 从 UIPickerView 中获取字符串值从数组中选择字符串值? ( swift/xcode)

ios - 如何在objective-c中使用cocoa pods升级magic record sdk

iOS:如果用户触发 PassKit 通行证,有没有办法在后台唤醒您的应用程序?

ios - 需要使用缓冲区保存和检索图像

iOS:在 UITabbarController 中显示登录 View Controller 或 "another" View Controller ?