gpt4 book ai didi

ios - 3 个文本字段,1 个 pickerview,但是,使用 pickerView 时,选择内容不会显示在文本字段中

转载 作者:行者123 更新时间:2023-11-29 05:51:56 24 4
gpt4 key购买 nike

UIPickerView 显示数据,但不会传输到 UITextField

我检查了其他帖子来比较我的代码,仍然找不到解决方案。非常感谢您的帮助。

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

@IBOutlet weak var hospNameField: UITextField!
@IBOutlet weak var teamNameField: UITextField!
@IBOutlet weak var mdNameField: UITextField!
@IBOutlet weak var selectionsPickerView: UIPickerView!

var hosp = ["hosp1", "hosp2", "hosp3"]
var team = ["team1", "team2", "team3"]
var mds = ["MD1", "MD2", "MD3"]

var itemSelected = ""
var textFieldSelected = UITextField()

weak var pickerView: UIPickerView?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self

hospNameField.inputView = pickerView
teamNameField.inputView = pickerView
mdNameField.inputView = pickerView

hospNameField.delegate = self
teamNameField.delegate = self
mdNameField.delegate = self

self.pickerView = pickerView
}

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

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

func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldSelected = textField
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if hospNameField.isFirstResponder {
return hosp.count
} else if teamNameField.isFirstResponder {
return team.count
} else if mdNameField.isFirstResponder {
return mds.count
}
return 0
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if hospNameField.isFirstResponder {
return hosp[row]
} else if teamNameField.isFirstResponder {
return team[row]
} else if mdNameField.isFirstResponder {
return mds[row]
}
return nil
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if teamNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
}
}

最佳答案

You have forgotten to subscribe the delegate and data sources to the PickerView..

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {

@IBOutlet weak var hospNameField: UITextField!
@IBOutlet weak var teamNameField: UITextField!
@IBOutlet weak var mdNameField: UITextField!
@IBOutlet weak var selectionsPickerView: UIPickerView!

var hosp = ["hosp1", "hosp2", "hosp3"]
var team = ["team1", "team2", "team3"]
var mds = ["MD1", "MD2", "MD3"]

var itemSelected = ""
var textFieldSelected = UITextField()

weak var pickerView: UIPickerView?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self

hospNameField.inputView = pickerView
teamNameField.inputView = pickerView
mdNameField.inputView = pickerView

hospNameField.delegate = self
teamNameField.delegate = self
mdNameField.delegate = self

self.pickerView = pickerView
/**
the below lines are mandatory for

pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)

to execute
*/
self.pickerView?.delegate = self
self.pickerView?.dataSource = self
}

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

func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldSelected = textField
}
// add this delegate and reload here
func textFieldShouldBeginEditing(_ textField: UITextField) ->
Bool {
self.pickerView?.reloadAllComponents()
return true
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if hospNameField.isFirstResponder {
return hosp.count
} else if teamNameField.isFirstResponder {
return team.count
} else if mdNameField.isFirstResponder {
return mds.count
}
return 0
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if hospNameField.isFirstResponder {
return hosp[row]
} else if teamNameField.isFirstResponder {
return team[row]
} else if mdNameField.isFirstResponder {
return mds[row]
}
return nil
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
/**
type here, need to check the responder for hospNameField also
you have executed teamNameField.isFirstResponder twice
*/
if hospNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
}
}

关于ios - 3 个文本字段,1 个 pickerview,但是,使用 pickerView 时,选择内容不会显示在文本字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55585677/

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