gpt4 book ai didi

ios - 在一个 UIViewController 中的 UIPickerViews 之间切换的问题

转载 作者:行者123 更新时间:2023-11-28 13:31:03 31 4
gpt4 key购买 nike

我有一个 UIViewController包含两个 UITextFields和两个应该填充 UIPickerViews 的 Picker 数组基于什么UITextField被选中。

UITextField pickerTextField选择了pickOption数组应该填充。当pickerTextField2选择了pickerOption2数组应该填充。

目前,当 pickerTextFieldpickerTextField2选择了相同的数组 pickerOption2正在为两者显示。

哪里写错了?

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

@IBOutlet weak var pickerTextField: UITextField!
@IBOutlet weak var pickerTextField2: UITextField!
var textField1: UITextField?
var pickOption = ["one", "two", "three", "four", "five"]
var pickOption2 = ["two", "four", "six", "eight", "ten"]

let pickerView = UIPickerView()

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

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField1 = textField
pickerView.reloadAllComponents()
pickerView.isHidden = false
return true
}



override func viewDidLoad() {
super.viewDidLoad()


pickerView.delegate = self
pickerTextField.delegate = self
pickerTextField2.delegate = self
pickerTextField.inputView = pickerView
pickerTextField2.inputView = pickerView

// Do any additional setup after loading the view, typically from a nib.

// ToolBar
let toolBar = UIToolbar()
toolBar.barStyle = .default
toolBar.isTranslucent = false
toolBar.tintColor = UIColor(red:0.78, green:0.26, blue:0.26, alpha:1.0)
toolBar.sizeToFit()

// Adding Button ToolBar
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.doneClick))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolBar.setItems([spaceButton, doneButton], animated: true)
toolBar.isUserInteractionEnabled = true
pickerTextField.inputAccessoryView = toolBar
pickerTextField2.inputAccessoryView = toolBar

}

@objc func doneClick() {
self.view.endEditing(true)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if textField1?.tag == 10 {
return pickOption.count
}
return pickOption2.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if textField1?.tag == 10 {
return pickOption[row]
}
return pickOption2[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if textField1?.tag == 10 {
textField1?.text = pickOption[row]
return
}
textField1?.text = pickOption2[row]

}
}

最佳答案

我会使用两个选择器

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

@IBOutlet weak var pickerTextField: UITextField!
@IBOutlet weak var pickerTextField2: UITextField!
// var textField1: UITextField? - not needed
var pickOption = ["one", "two", "three", "four", "five"]
var pickOption2 = ["two", "four", "six", "eight", "ten"]

let pickerView1 = UIPickerView()
let pickerView2 = UIPickerView()

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

// This should not be needed
//func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// textField1 = textField
// pickerView.reloadAllComponents()
//pickerView.isHidden = false
// return true
// }

override func viewDidLoad() {
super.viewDidLoad()

pickerView1.delegate = self
pickerView2.delegate = self
pickerTextField.delegate = self // This may not be needed anymore
pickerTextField2.delegate = self // This may not be needed anymore
pickerTextField.inputView = pickerView1
pickerTextField2.inputView = pickerView2

// Do any additional setup after loading the view, typically from a nib.

// ToolBar
//..... all your code

// Adding Button ToolBar
// ... more of your code

}

@objc func doneClick() {
self.view.endEditing(true)
}


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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView = pickerView1 {
return pickOption.count
}
return pickOption2.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pickerView1 {
return pickOption[row]
}
return pickOption2[row]
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pickerView1 {
pickerTextField.text = pickOption[row]
} else {
pickerTextField2.text = pickOption2[row]
}
}
}

关于ios - 在一个 UIViewController 中的 UIPickerViews 之间切换的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57327233/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com