xcode11 如何在 PickerView 中为选定选项提供操作。这样当我在所选数组中选择一个选项时,我可以对其执行操作

Posted

技术标签:

【中文标题】xcode11 如何在 PickerView 中为选定选项提供操作。这样当我在所选数组中选择一个选项时,我可以对其执行操作【英文标题】:xcode11 how to give a selected option an action, in a PickerView. So that when I select an option in the selected array, I can perform an action to it 【发布时间】:2020-08-25 16:07:49 【问题描述】:

例如,我在 PickerView 中进行选择(国家:苏格兰,城市:阿伯丁)一旦我在选择器视图中选择了这两个选项,我会选择一个按钮并让该操作将我带到另一个页面,其中包含有关苏格兰、阿伯丁的信息具体来说。

我想要做的是让我的 PickerView 选项将我带到另一个页面,其中包含特定于这些选项的数据。这是我到目前为止所编写的代码。

import UIKit

class GrapplingViewController: UIViewController 

    @IBOutlet weak var trainingTextField: UITextField!
    @IBOutlet weak var areaTextField: UITextField!
    @IBOutlet weak var focusTextField: UITextField!
    
            
   
    
    let training = ["Skill Reciting", "Sparring"]
            let area = ["Takedowns", "Top Battle", "Bottom Battle", "Survival"]
            let focus = ["Full Training", "Specific Training"]
    
           
            
            var trainingPickerView = UIPickerView ()
            var areaPickerView = UIPickerView ()
            var focusPickerView = UIPickerView()
            
            
            
   override func viewDidLoad() 
          super.viewDidLoad()
          

          title = "Grappling"
      
                
                
                trainingTextField.inputView = trainingPickerView
                areaTextField.inputView = areaPickerView
                focusTextField.inputView = focusPickerView
                
                
    
                trainingTextField.placeholder = "Select training type"
                areaTextField.placeholder = "Select start position"
                focusTextField.placeholder = "Select your focus"
    
                
                
                trainingTextField.textAlignment = .center
                areaTextField.textAlignment = .center
                focusTextField.textAlignment = .center
    
              
                
    
                trainingPickerView.delegate = self
                trainingPickerView.dataSource = self
            areaPickerView.delegate = self
            areaPickerView.dataSource = self
                focusPickerView.delegate = self
            focusPickerView.dataSource = self
                
                
                
                trainingPickerView.tag = 1
                areaPickerView.tag = 2
                focusPickerView.tag = 3
          
               
    
            
            
        

        extension GrapplingViewController: UIPickerViewDataSource, UIPickerViewDelegate 
            
            func numberOfComponents(in pickerView: UIPickerView) -> Int 
                return 1
            
            
            func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
                switch pickerView.tag 
                case 1:
                    return training.count
                    case 2:
                        return area.count
                case 3:
                    return focus.count
                default:
                    return 1
                
            
            
           
            
            func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
                switch pickerView.tag 
                case 1:
                    return training[row]
                case 2:
                        return area[row]
                case 3:
                    return focus[row]
                default:
                    return "Data not found"
            
            
            
            
            
            func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
                
                switch pickerView.tag 
                    case 1:
                        trainingTextField.text = training[row]
                        trainingTextField.resignFirstResponder()
                    case 2:
                           areaTextField.text = area[row]
                            areaTextField.resignFirstResponder()
                    case 3:
                        focusTextField.text = focus[row]
                        focusTextField.resignFirstResponder()
                    default:
                        return
                
            
    

         @IBAction func startButton(_ sender: UIButton) 
              
               





【问题讨论】:

【参考方案1】:

假设您只有在选择器视图中选择了所有三个选项后才想进入下一个视图控制器,您可以试试这个。

每次使用didSelectRow 方法时,保存已选择的选项。例如:我将拥有三个属性,selectedTrainingselectedAreaselectedFocus 各有一个。

每次调用didSelectRow,在case语句中,检查其他两个变量是否有值。如果是,那么,假设GrapplingViewController 连接到另一个viewContoller,您可以调用performSegue 方法。

【讨论】:

以上是关于xcode11 如何在 PickerView 中为选定选项提供操作。这样当我在所选数组中选择一个选项时,我可以对其执行操作的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的 podfile 中为我的 Xcode 项目指定多个目标?

如何在 xCode 4.2 中为两个方向正确设置 iPad 启动图像

在pickerview(Swift 3,Xcode)中使(第一)行不可选择

如何在 Xcode 7 中为 Homekit 添加 app_name.entitlements 文件

多个 UIPickerview 错误

如何在 xcode 7 中为 xcode 8 中的标签添加更多字体大小,它在字体旁边显示“+”符号