gpt4 book ai didi

ios - Swift - 将 pickerView 添加到 UIViewController 中的文本字段

转载 作者:行者123 更新时间:2023-11-30 13:07:22 25 4
gpt4 key购买 nike

我在选项以及使用 @IBOutlet 设置 View 和 subview 方面遇到了一些困难。我目前正在尝试创建一个 Controller 来将用户添加到数据库中。我的字段之一与用户的性别相关,因此我希望将其链接到 UIPickerView。 我没有使用 Storyboard 。

我从代码中删除了所有其他字段,使其变得更短且可读,但它还将具有其他 UIPickerView 字段(例如生日)。

有人可以帮助我编写代码,以便我的文本字段在选择时显示 UIPickerView 吗?

class AddUserController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {

var picker: UIPickerView = UIPickerView()

var genderData = ["Male", "Female"]

// Containers
let inputsContainerView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.whiteColor()
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.masksToBounds = true
return view
}()

// Subviews
@IBOutlet weak var genderTextField: UITextField! = {
var gender = UITextField()
gender.placeholder = "Gender"
gender.translatesAutoresizingMaskIntoConstraints = false
gender.leftViewMode = UITextFieldViewMode.Always
gender.autocorrectionType = .No

let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: 120, height: 30))
lbl.text = "GENDER"
lbl.font = UIFont.boldSystemFontOfSize(12)
gender.leftView = lbl
return gender
}()

override func viewDidLoad() {
super.viewDidLoad()

picker = UIPickerView()

picker.dataSource = self
picker.delegate = self
picker.hidden = true

genderTextField?.delegate = self
genderTextField?.inputView = picker
genderTextField?.text = genderData[0]

view.backgroundColor = UIColor(white: 0.95, alpha: 1)

view.addSubview(inputsContainerView)

setupInputsContainerView()
}

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return genderData.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return genderData[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
genderTextField.text = genderData[row]
self.view.endEditing(true)
picker.hidden = true
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
picker.hidden = false
return true
}

// Setup all the views
func setupInputsContainerView() {
inputsContainerView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true
inputsContainerView.topAnchor.constraintEqualToAnchor(view.bottomAnchor, constant: 12).active = true
inputsContainerView.widthAnchor.constraintEqualToAnchor(view.widthAnchor, constant: -24).active = true
inputsContainerView.heightAnchor.constraintEqualToConstant(40).active = true

inputsContainerView.addSubview(genderTextField)

genderTextField.leftAnchor.constraintEqualToAnchor(inputsContainerView.leftAnchor, constant: 12).active = true
genderTextField.topAnchor.constraintEqualToAnchor(inputsContainerView.topAnchor).active = true
genderTextField.widthAnchor.constraintEqualToAnchor(inputsContainerView.widthAnchor).active = true
genderTextField.heightAnchor.constraintEqualToAnchor(inputsContainerView.heightAnchor).active = true
}
}

最佳答案

每次调用时都会创建enderTextField。

genderTextField?.delegate = self
genderTextField?.inputView = picker
genderTextField?.text = genderData[0]

所有这些都是不同的对象

inputsContainerView.addSubview(genderTextField)

您在这里添加的内容也不同。因此,您添加到 View 中的文本字段和您添加的委托(delegate)/数据源都是不同的。一种快速的解决方案是移动

genderTextField?.delegate = self
genderTextField?.inputView = picker
genderTextField?.text = genderData[0]

此行之后的这些代码inputsContainerView.addSubview(genderTextField)

关于ios - Swift - 将 pickerView 添加到 UIViewController 中的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39180443/

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