在出口集合文本字段上使用 pickerView
Use a pickerView on a outlet collection textfield
我的下面代码是一个 uitextfiled,它使用选择器视图来显示 a 和 b。我想要做的就是让所有使用插座集合的文本字段都显示相同的选择器视图。 Textfield 是单个 textField,而 mutlipleTextifeld 是我要使用的插座集合。我只想用 mutlipleTextifield 替换 textField。
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let picker = UIPickerView()
let country = ["a","b"]
@IBOutlet var mutlipleTextifeld: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
for textFieldObject in mutlipleTextifeld
{
textFieldObject.inputView = picker
}}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return country.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return country[row]
}
public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
mutlipleTextifeld.text = country[row]
self.view.endEditing(false)
}}
在您的 viewDidLoad 方法中使用以下代码。就是这样!
let count : int = 0
for textField in mutlipleTextifeld {
textField.inputView = picker
textField.tag = count
count +=1
}
在您的控制器中取一个名为 textFiledTag
的全局变量
每当您点击文本字段时,都会调用以下方法 -
textFieldDidBeginEditing
int 这个方法写下一行
textFieldTag = textField.tag
现在您可以通过获取具有全局标签值的标签来设置选择器视图的文本。
希望这对您有所帮助..!
首先将pickerView
的dataSource
和Delegate
设置为self
即ViewController
class。正如我所见,你没有。所以你的 viewDidLoad()
会有点像这样:
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
print (textFields.count)
for textFieldObject in textFields
{
textFieldObject.inputView = picker
}
}
你的 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
delegate
会是这样的:
public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
for textField in textFields {
if textField.isEditing {
textField.text = country[row]
}
}
self.view.endEditing(false)
}
输出
我的下面代码是一个 uitextfiled,它使用选择器视图来显示 a 和 b。我想要做的就是让所有使用插座集合的文本字段都显示相同的选择器视图。 Textfield 是单个 textField,而 mutlipleTextifeld 是我要使用的插座集合。我只想用 mutlipleTextifield 替换 textField。
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let picker = UIPickerView()
let country = ["a","b"]
@IBOutlet var mutlipleTextifeld: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
for textFieldObject in mutlipleTextifeld
{
textFieldObject.inputView = picker
}}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return country.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return country[row]
}
public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
mutlipleTextifeld.text = country[row]
self.view.endEditing(false)
}}
在您的 viewDidLoad 方法中使用以下代码。就是这样!
let count : int = 0
for textField in mutlipleTextifeld {
textField.inputView = picker
textField.tag = count
count +=1
}
在您的控制器中取一个名为 textFiledTag
每当您点击文本字段时,都会调用以下方法 -
textFieldDidBeginEditing
int 这个方法写下一行
textFieldTag = textField.tag
现在您可以通过获取具有全局标签值的标签来设置选择器视图的文本。
希望这对您有所帮助..!
首先将pickerView
的dataSource
和Delegate
设置为self
即ViewController
class。正如我所见,你没有。所以你的 viewDidLoad()
会有点像这样:
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
print (textFields.count)
for textFieldObject in textFields
{
textFieldObject.inputView = picker
}
}
你的 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
delegate
会是这样的:
public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
for textField in textFields {
if textField.isEditing {
textField.text = country[row]
}
}
self.view.endEditing(false)
}
输出