如何为所有 PickerViews 创建单个完成和取消按钮?

Posted

技术标签:

【中文标题】如何为所有 PickerViews 创建单个完成和取消按钮?【英文标题】:How to create single Done and Cancel button for all the PickerViews? 【发布时间】:2020-07-27 14:36:38 【问题描述】:

我想要实现的是完成按钮应该将特定的选择器视图行值保存到特定的 UITextField。 我在一个视图控制器中有两个pickerview。

override func viewDidLoad() 
     textField1.inputView = pickerview1
     textField2.inputView = pickerview2
     let toolBar = UIToolbar()
     toolBar.barStyle = UIBarStyle.default
     toolBar.isTranslucent = true
     toolBar.sizeToFit()
     let cancelButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.cancel, target: nil, action: #selector(self.cancelPicker))
     let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
     let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: self, action: #selector(self.donePicker))
     toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
     toolBar.isUserInteractionEnabled = true
     textField1.inputAccessoryView = toolBar
     textField2.inputAccessoryView = toolBar   
    
  @objc func cancelPicker() 
     //cancel that particular pickerview
   
    @objc func donePicker() 
      // get that particular value in textfield
    

文本字段和选择器视图都作为 IBOutlets 连接。

【问题讨论】:

【参考方案1】:

您可以检测哪个文本字段已获得焦点并存储对它的引用(我存储了它的索引),当您点击工具栏上的完成或取消按钮时,您可以更改该特定文本字段的值。

我将您的代码更改如下。

class ViewController: UIViewController , UITextFieldDelegate 
    
    @IBOutlet var textFields : [UITextField]!
    @IBOutlet var pickerviews : [UIPickerView]!
    
    
    let array = ["one" , "two" , "three"]
    
    var selectedTextfieldIndex : Int?
    var selectedTitle : String?
    
    override func viewDidLoad() 
        
        for pickerview in pickerviews
        
            pickerview.delegate = self
            pickerview.dataSource = self
        
        
        for textfield in textFields
        
            textfield.delegate = self
        
        
        
        let toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.default
        toolBar.isTranslucent = true
        toolBar.sizeToFit()
        let cancelButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.cancel, target: nil, action: #selector(self.cancelPicker))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: self, action: #selector(self.donePicker))
        toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
        toolBar.isUserInteractionEnabled = true
        textFields[0].inputAccessoryView = toolBar
        textFields[1].inputAccessoryView = toolBar
        
        
        textFields[0].inputView = pickerviews[0]
        textFields[1].inputView = pickerviews[1]
    
    
    func textFieldDidBeginEditing(_ textField: UITextField) 
        
        for ( index , item) in textFields.enumerated()
        
            if textField == item
            
                selectedTextfieldIndex = index
            
        
    
    
    
    @objc func cancelPicker() 
        
        guard let index = selectedTextfieldIndex else  return 

        textFields[index].text = nil
        selectedTextfieldIndex = nil
        textFields[index].endEditing(true)

    
    
    @objc func donePicker() 
        
        guard let index = selectedTextfieldIndex else  return 
        
        textFields[index].text = selectedTitle
        selectedTextfieldIndex = nil
        textFields[index].endEditing(true)

    
    




extension ViewController : UIPickerViewDelegate , UIPickerViewDataSource

    func numberOfComponents(in pickerView: UIPickerView) -> Int 
        return 1
    
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        return array.count
    
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? 
        return array[row]
    
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        self.selectedTitle = array[row]
    

【讨论】:

以上是关于如何为所有 PickerViews 创建单个完成和取消按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何为单个项目中运行的每个批次创建单独的日志文件?

如何为完成端口创建多个线程?

使用 SIGEV_THREAD 时,如何为所有 timer_create 回调重用单个线程?

如何为 MongoDB 集合中的所有文档选择单个字段?

在Linux上如何为单个用户设置某个文件的读、写、执行权限?

如何为自定义帖子创建单个模板