I am new to Swift. To fill some Textfields in my app, I want to use one picker view which changes its population depending on which textfield I want to fill. My Problem is, that the function textFieldShouldBeginEditing
seems not to work. The variable textFieldName
doesn't change its name. Unfortunately all the code I found here is only for Objective-C. I hope you can help me bring this code to work.
Here is my code:
@IBOutlet weak var Stadt: UITextField!
@IBOutlet weak var Untergrund: UITextField!
@IBOutlet weak var Groesse: UITextField!
@IBOutlet weak var AnzToreKoerbe: UITextField!
@IBOutlet weak var Platzart: UITextField!
var UntergrundArray = ["Kunstrasen Sand", "Kunstrasen Granulat", "Rasen", "Tartan", "Teer"]
var StadtArray = ["Norderstedt", "Hamburg", "Berlin", "München"]
var GroesseArray = ["1 vs 1", "2 vs 2", "3 vs 3", "4 vs 4", "5 vs 5", "6 vs 6", "7 vs 7"]
var AnzTorKoerbe = ["1", "2", "3", "4"]
var Art = ["Fußball", "Basketball"]
var textFieldName = "Stadt1"
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
if textField.tag == 1{
textFieldName = "Stadt1"
}else if textField.tag == 2{
textFieldName = "Untergrund1"
}else if textField.tag == 3 {
textFieldName = "Groesse1"
}else if textField.tag == 4 {
textFieldName = "AnzToreKoerbe1"
}else if textField.tag == 5 {
textFieldName = "Platzart1"
}
return true;
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (textFieldName.isEqual("Stadt1")) {
return StadtArray.count
}else if (textFieldName.isEqual("Untergrund1")) {
return UntergrundArray.count
}else if (textFieldName.isEqual("Groesse1")) {
return GroesseArray.count
}else if (textFieldName.isEqual("AnzToreKoerbe1")) {
return AnzTorKoerbe.count
}else {
return Art.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (textFieldName.isEqual("Stadt1")) {
return StadtArray [row]
}else if (textFieldName.isEqual("Untergrund1")) {
return UntergrundArray [row]
}else if (textFieldName.isEqual("Groesse1")) {
return GroesseArray [row]
}else if (textFieldName.isEqual("AnzToreKoerbe1")) {
return AnzTorKoerbe [row]
}else {
return Art [row]
}
}
You need to called reloadAllComponents
at the end of the textFieldShouldBeginEditing
method.
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
if textField.tag == 1{
textFieldName = "Stadt1"
}else if textField.tag == 2{
textFieldName = "Untergrund1"
}else if textField.tag == 3 {
textFieldName = "Groesse1"
}else if textField.tag == 4 {
textFieldName = "AnzToreKoerbe1"
}else if textField.tag == 5 {
textFieldName = "Platzart1"
}
somePickerViewProperty.reloadAllComponents()
return true;
}
Replace somePickerViewProperty
with the name of your UIPickerView
outlet.
There also a much simpler way to write your code:
var UntergrundArray = ["Kunstrasen Sand", "Kunstrasen Granulat", "Rasen", "Tartan", "Teer"]
var StadtArray = ["Norderstedt", "Hamburg", "Berlin", "München"]
var GroesseArray = ["1 vs 1", "2 vs 2", "3 vs 3", "4 vs 4", "5 vs 5", "6 vs 6", "7 vs 7"]
var AnzTorKoerbe = ["1", "2", "3", "4"]
var Art = ["Fußball", "Basketball"]
var currentData = StadtArray
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
if textField.tag == 1{
currentData = StadtArray
}else if textField.tag == 2{
currentData = UntergrundArray
}else if textField.tag == 3 {
currentData = GroesseArray
}else if textField.tag == 4 {
currentData = AnzTorKoerbe
}else if textField.tag == 5 {
currentData = Art
}
somePickerViewProperty.reloadAllComponents()
return true;
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return currentData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return currentData[row]
}
Have you set the UITextField delegate to self
? I can't see it in the code you provided and the methods such as textFieldShouldBeginEditing
won't be called if the delegate is not set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.