The title says it all. I am trying to assign a value (gender) that the user will select from a UIPicker and then the value will appear as the UITextField input using Swift.
标题说明了一切。我正在尝试分配一个值(性别),用户将从一个UIPicker中选择,然后这个值将作为UITextField输入使用Swift来显示。
Here is my code:
这是我的代码:
class RegistrationViewController: UIViewController, UIPickerViewDelegate, UITextFieldDelegate {
@IBOutlet var getGender: UIPickerView! = UIPickerView()
@IBOutlet var genderTextField: UITextField! = nil
let gender = ["Male", "Female", "Other"]
override func viewDidLoad() {
super.viewDidLoad()
self.genderTextField.inputView = pickerView
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return gender.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return gender[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
genderTextField.text = "\(gender[row])"
}
I am aware that this could be achieved if instead of a textfield I use a label and format it to look as a textfield. Then changing the value of the label.
我知道,如果我使用标签而不是textfield,并将其格式化为textfield,这是可以实现的。然后改变标签的值。
I would also like to know how to show/hide the UIPicker when the label/textfield is clicked. Could anyone help me out with that?
我还想知道如何在标签/textfield被单击时显示/隐藏UIPicker。谁能帮我一下吗?
Thank you so much for your advice!
非常感谢您的建议!
Cheers.
欢呼。
Update:
更新:
I have done everything but the only thing that the compiler complains about is the line with the comment //ERROR. As I mentioned, it does not accept only this:
我已经做了所有的事情,但是编译器唯一要抱怨的是带有注释//错误的行。正如我所提到的,它不仅接受这一点:
self.genderTextField.inputView = pickerView //ERROR -> Requires more declarations! Read update section below!
It asks me to provide extra declarations such as:
它要求我提供额外的声明,例如:
pickerView!(pickerView: UIPickerView?, didSelectRow: Int, inComponent: Int)
pickerView!(pickerView: UIPickerView?, numberOfRowsInComponent: Int)
There are a couple other suggestions from Xcode but I do not know which one to use and how.
Xcode中还有其他一些建议,但我不知道该使用哪一个以及如何使用。
Help please!
请帮助!
Thanks!
谢谢!
2 个解决方案
#1
3
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textField.text = "\(gender[row])"
}
And to show/hide the picker (in viewDidLoad)
并显示/隐藏picker(在viewDidLoad中)
self.textField.inputView = pickerView
#2
-1
Try this: self.txtSelectareRate.inputView = UIPickerView()
试试这个:self.txtSelectareRate。视图=另UIPickerView()
#1
3
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textField.text = "\(gender[row])"
}
And to show/hide the picker (in viewDidLoad)
并显示/隐藏picker(在viewDidLoad中)
self.textField.inputView = pickerView
#2
-1
Try this: self.txtSelectareRate.inputView = UIPickerView()
试试这个:self.txtSelectareRate。视图=另UIPickerView()