我想在我的UITextField中保留相同的占位符文本,即“位置”。触摸UITextField后,我想要显示UIPickerView的第一个值。现在用户必须向下滚动然后备份以便在UITextField中显示该值。我想要在UIPickerView打开后立即在UITextField中显示该值。 unselected, selected-UITextField still shows no value
override func viewDidLoad() {
let thePicker = UIPickerView()
thePicker.delegate = self
location.inputView = thePicker
thePicker.selectRow(1, inComponent: 0, animated: true)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickOptions.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickOptions[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
location.text = pickOptions[row]
}
答案 0 :(得分:2)
实施textFieldDidBeginEditing
文本字段委托方法,并设置文本字段的文本(如果它还没有值)。
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.text!.isEmpty {
// set the text as needed
}
}
答案 1 :(得分:0)
import UIKit
class ActivityPersonDetail : UIViewController,UITextFieldDelegate,UIPickerViewDataSource, UIPickerViewDelegate {
let salutations = ["1", "2", "3","4","5","6","7","8","9","10","11","12"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return salutations.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return salutations[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerTF.text = salutations[row]
picker.isHidden = true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.pickerTF {
picker.isHidden = false
textField.endEditing(true)
}
}
lazy var picker: UIPickerView = {
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.translatesAutoresizingMaskIntoConstraints = false
pickerView.backgroundColor = .white
pickerTF.inputView = pickerView
return pickerView
}()
let arrowImage: UIImageView = {
let image = UIImageView()
image.translatesAutoresizingMaskIntoConstraints = false
image.image = #imageLiteral(resourceName: "down_arrow")
image.contentMode = .scaleAspectFit
return image
} ()
lazy var pickerTF: UITextField = {
let tf = UITextField()
tf.delegate = self
tf.translatesAutoresizingMaskIntoConstraints = false
tf.textColor = .black
tf.font = .systemFont(ofSize: 24, weight: .medium)
tf.placeholder = salutations[0]
return tf
}()
//
// MARK :- viewDidLoad ============================================================================
//
private var pickerView: UIPickerView!
override func viewDidLoad() {
super .viewDidLoad()
view.backgroundColor = .white
setupNavigationBar()
setupViews()
setupAutoLayout()
}
// ================ setupNavigationBar =============
func setupNavigationBar(){
title = ""
hideBackTitle()
navigationController?.navigationBar.barTintColor = .navBar
}
func hideBackTitle() {
let backbarItem = UIBarButtonItem()
backbarItem.title = ""
navigationItem.backBarButtonItem = backbarItem
}
// ================ setupViews =============
func setupViews(){
view.addSubview(pickerTF)
pickerTF.addSubview(arrowImage)
view.addSubview(picker)
picker.isHidden = true
}
// ================ setupAutoLayout =============
func setupAutoLayout() {
pickerTF.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 80).isActive = true
pickerTF.topAnchor.constraint(equalTo: view.topAnchor, constant: 80).isActive = true
pickerTF.heightAnchor.constraint(equalToConstant: 60).isActive = true
pickerTF.widthAnchor.constraint(equalToConstant: 70).isActive = true
arrowImage.rightAnchor.constraint(equalTo: pickerTF.rightAnchor, constant: -15).isActive = true
arrowImage.topAnchor.constraint(equalTo: pickerTF.topAnchor, constant: 16).isActive = true
arrowImage.widthAnchor.constraint(equalToConstant: 22).isActive = true
arrowImage.heightAnchor.constraint(equalToConstant: 22).isActive = true
picker.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
picker.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
picker.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
picker.heightAnchor.constraint(equalToConstant: 100).isActive = true
}
}
答案 2 :(得分:0)
我将以rmaddy的答案为基础,并在评论中添加更多细微差别和对问题的答案。
主要答案在于第一个扩展名,其余代码概述了所有工作所需的类结构和组件。
class ViewController {
private var selectedItem: String = ""
private let pickerList = [
"First item",
"Second item",
"Third item"
]
override func viewDidLoad() {
super.viewDidLoad()
pickerTextField.delegate = self
//...
// Layout setup
//...
}
private let pickerTextField: UITextField = {
let textField = UITextField()
textField.tintColor = .clear // hides cursor
return textField
}()
//...
// UI Elements including picker
//...
}
extension ViewController: UITextFieldDelegate {
internal func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == pickerTextField, let textFieldText = textField.text, textFieldText.isEmpty {
pickerTextField.text = pickerList.first // fills textfield before any selection is made
}
}
}
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
//...
// PickerView delegate methods
//...
}
答案 3 :(得分:0)
@IBOutlet weak var tF: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
tF.delegate = self
}
func textFieldDidEndEditing(_ textField: UITextField) {
textField.text = pickOptions[row]
}