代码之家  ›  专栏  ›  技术社区  ›  user979331

Swift 4-带UIPickerView的自定义UICollectionReusableView

  •  0
  • user979331  · 技术社区  · 5 年前

    我建立了一个习惯 UICollectionReusableView 像这样的班级:

    protocol ReviewFooterDelegate: NSObjectProtocol {
        func submitReviewDelegate(text: String, rating: Float)
    }
    
    class ReviewFooterCell: UICollectionReusableView {
    
        @IBOutlet var text: UITextView!
        @IBOutlet var submitButton: UIButton!
        @IBOutlet var rating: CosmosView!
        @IBOutlet var problem: UITextField!
        var reviewFooterDelegate: ReviewFooterDelegate!
    
        @IBAction func submitButtonPressed(_ sender: Any) {
    
            reviewFooterDelegate.submitReviewDelegate(text: self.text.text, rating: Float(rating.rating))
        }
    }
    

    UITapGestureRecognizer UITextField UIPickerView ,这是我的控制器中使用此自定义类的内容:

    viewForSupplementaryElementOfKind 方法:

        let footerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "ReviewsFooter", for: indexPath) as! ReviewFooterCell
        footerView.reviewFooterDelegate = self
        let problemGesture = UITapGestureRecognizer(target: self, action: #selector(problemTapped))
        footerView.problem.addGestureRecognizer(problemGesture)
        return footerView
    

    下面是problemTapped方法:

    @objc func problemTapped(sender : UITapGestureRecognizer) {
    
            pickerView.delegate = self
            pickerView.dataSource = self
    
            let toolBar = UIToolbar()
            toolBar.barStyle = UIBarStyle.default
            toolBar.isTranslucent = true
            toolBar.sizeToFit()
    
            let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(ProfessionalReviews.donePicker))
            doneButton.tintColor = UIColor(red: 30.0 / 255.0, green: 53.0 / 255.0, blue: 94.0 / 255.0, alpha: 1.0)
    
            let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: nil, action: nil)
    
            let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItem.Style.plain, target: self, action: #selector(ProfessionalReviews.cancelPicker))
            cancelButton.tintColor = UIColor(red: 30.0 / 255.0, green: 53.0 / 255.0, blue: 94.0 / 255.0, alpha: 1.0)
    
            toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
            toolBar.isUserInteractionEnabled = true
    
            /*
            problem.inputAccessoryView = toolBar
            view.addSubview(problem)
            problem.inputView = pickerView
            problem.becomeFirstResponder()
            */
        }
    

    现在我陷入了下一步该做什么,我该如何设置 输入框 作为第一反应者在我的定制课上?

    0 回复  |  直到 5 年前
        1
  •  2
  •   Zion    5 年前

    正如其他人所评论的,您不需要UITapGestureRecognizer就可以完成您的要求:

    UITextField以显示UIPickerView。

    而是设置textField的 inputView 给你的挑夫。下面是一个代码示例。视图控制器有一个textField,点击时会显示pickerView。此项目在github上;请参阅下面的参考资料。

    class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
        @IBOutlet weak var pickerTextField: UITextField!
    
        var pickOption = ["one", "two", "three", "four", "five"]
    
        var pickerView: UIPickerView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let pickerView = UIPickerView()
    
            // Assign the pickerView delegate
            pickerView.delegate = self
    
            // Assign the textField's inputView to be the picker
            pickerTextField.inputView = pickerView
        }
    
        // UIPickerViewDelegate methods
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }
    
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return pickOption.count
        }
    
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            return pickOption[row]
        }
    
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            pickerTextField.text = pickOption[row]
        }
    }
    

    由于您的textView包含在UICollectionReusableView中,因此您可以在单元格类中使用一个函数来设置textView的inputView,如下所示:

    class ReviewFooterCell: UICollectionReusableView {
        func setTextFieldInputView(pickerView: UIPickerView) {
            problem.inputView = pickerView   
        }    
    }
    

    alt text

    工具书类