In my app, I have a UITextField and UIPickerView acting as dropdown, when user clicks on text field, he has to select his gender (Male or Female. I just want to read the value / string which user has selected in text field.Could some one please help me here
#IBAction func textChange(_ sender: UITextField) {
if userGenderTextField (Something has to here, Not sure what exactly)== "Male" {
print("Hello Sir")
} else {
print("Hello Madam")
}
}
You just have to use the .text method of UITextField
#IBAction func textChange(_ sender: UITextField) {
if sender.text == "Male" {
print("Hello Sir")
} else {
print("Hello Madam")
}
}
If you have several text fields connected to the same IBAction, you also need to check which text field called the function.
This sounds like a round-a-bout way of doing it.
Presumably at some point you're detecting which item in the UIPickerView the user selects and setting the text on the UITextField?
If so, why not store this value as a property. Then you always have access to it.
It doesn't seem like the best idea to have to inspect the text value of a UITextField when you want to know the state of play.
First of all, create a uIPickerView programmatically and set its dataSource and delegate properties to some objects that conform to the UIPickerViewDataSource and UIPickerViewDelegate respectively. The best approach is to have the object that controls the UITextField and UIPickerView conform to both these to protocols since you will be needing direct access to your textField and pickerView.
UIPickerViewDataSource is to control the data that is displayed in the picker.
UIPickerViewDelegate is to receive events that happen on the picker (e.g when the user selects an option).
Once you have your pickerView ready and set up you have your pickerView as the inputView of your textField
textField.inputView = pickerView
this way, when someone clicks on the textField, instead of presented the keyboard, you will be presented with you pickerView complete with your data.
On UIPickerViewDelegate there is a function called:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
that gets called everytime you select an option on the pickerView.
Inside that function you can set the text of your textField and have it accessible elsewhere:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
textField.text = yourData[component][row]
}
In if statement you put textfieldname.text== male print male else female
Related
I want to update a UITextField inside a UIAlertController when my UIPickerView scrolls.
I tried to add a tag on my textfield:
myTextField.addTextField(configurationHandler: { (textField) -> Void in
textField.inputView = self.myPickerView
textField.tag = 2
textField.delegate = self
})
For the pickerView delegate method didSelectRow I declared a ivar pickerViewName to store the row's name:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerViewName = myArray[row].name
//Update UITextField text here, but unable to get it's tag.
}
and on the textFieldDidBeginEditing delegate method:
func textFieldDidBeginEditing(_ textField: UITextField) {
if (textField.tag == 2)
{
textField.text = pickerViewName!
}
}
It isn't working however. When I scrolled up and down the textfield does not update. Much appreciated if you could help me point out where my mistake is! Thank you.
In your code you are doing following: if the picker scrolls you assign a new name to the variable. However you never call update for the textfield. Your name is assigned after you start editing text field. I am not sure if this is desired, otherwise you will have to call it manually.
I was wondering how to get a UIPickerView to slide up from the bottom of the screen after tapping on a drop-down style button. Like in the image below:
I've run into this type of picker views a lot in the apps that I use regularly, so honestly I was expecting to easily find this picker view by setting the UIPickerView's style property, or something like that. Is this even a UIPickerView or do I have to create this kind of view manually?
One way of doing this is to have a normal UITextField and then assign a UIPickerView as the inputView of that textfield. That way, instead of a keyboard appearing when you tap your textfield, you get your pickerview.
Example
First declare a normal UIPickerView instance:
let yourPicker = UIPickerView()
and an outlet to a UITextField:
#IBOutlet weak var yourTextField: UITextField!
In your viewDidLoad you tie the elements together
yourPicker.delegate = self
yourPicker.dataSource = self
yourTextField.inputView = yourPicker
And then you need to implement the required methods of UIPickerViewDelegate and UIPickerViewDataSource
Finally. In pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) you update the value of your textfield:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
yourTextField.text = yourDataArrayUsedInThePicker[row]
}
Read more
Description of inputView
https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html
A way better explaination than mine:
Show UIPickerView text field is selected, then hide after selected
Hope that helps you.
Is there a possibility to get UITextField from UIPickerView? I set my UIPickerView as inputView for text filed. I have few text fields on the screen and I want to simple get current text field that shown picker view.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
// something like let textField = pickerView.requestedTextField
}
Is there any way to do it?
If you have multiple textField that show pickerView then create one more instance of textField in your viewController like this and use this object inside textFieldDidBeginEditing and assign the reference of that textField to that tempTextField.
var selectedTextField: UITextField = UITextField()
func textFieldDidBeginEditing(textField: UITextField!) {
self.selectedTextField = textField
}
Now in didSelectRow method of PickerView use this textField o know the current editing field.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if self.selectedTextField == countryField {
//Set text for countryField
}
else self.selectedTextField == stateField {
//Set text for stateField
}
}
Note: For example I have used countryField and stateField you can use textField for that you want to set text.
Set same tag to your pickerview and textfield both.
now you can get your tag in didSelectRow like,
let myTag = pickerView.tag
and then from myTag you can get your textfield something like,
let myTextField = self.view.viewWithTag(myTag) as! UITextField
As user Lion rightly pointed out in his answer, you can get the corresponding UITextField by giving both the pickerView and textField a tag. However, the issue with giving them the same tag is that viewWithTag may return the UIPickerView itself, depending on the order of the subviews.
As this is unreliable, a better solution would be to give the UITextField the negative of the tag given to the UIPickerView (eg. 1 and -1). To get the UITextView, you would do something like this in your method, once you have assigned your picker views and text views opposite tags:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let textField = view.viewWithTag(-pickerView.tag) as! UITextField
//Use textField
}
I would like to know if i could get the selected String of a PickerView because when using the method func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) i only know the row selected and what i am using is if i know the row i know the position on the array i used to load the PickerViews but i wanted to do this pickerView.text to do something like this
if (pickerView.text == "hello"){
doSomething(pickerView.text);
}
Because now i have to do much more code lines to do something like this
Sorry i forget to mention, i have 3 PickerView and to know the String is in the third picker view i have to see what row is selected in each one
i only know the row selected and what i am using is if i know the row i know the position on the array
And that's the answer. Don't fight the framework; use it. The framework uses Model-View-Controller. The UIPickerView is view; it has no data. You have the data, and given the row, you can fetch it from the model.
extension UIPickerView {
func selectedTitleForComponent(component: Int) -> String? {
let row = selectedRowInComponent(component)
return dataSource.pickerView(pickerView, titleForRow:row, forComponent:component)
}
}
Then:
if pickerView.selectedTitleForComponent(0) == "hello" {
...
}
I am currently working on a small project and i have a viewController that has 4 textFields which 3 work ok. They take String objects. However, the 4th textField is supposed to bring up a UIPickerView with 4 selectable items.
So far this is what i have in my controller that implements this:
#IBOutlet var pickerTextfield: UITextField!
#IBOutlet var itemPicker: UIPickerView! = UIPickerView()
The pickerTextfield is the UITextField object that is the 4th field.
The itemPicker is an unlinked UIPickerView that i want to create programatically.
Right below these properties, i have an array of items for the UIPickerView object:
var seasonalItems = ["Spring", "Summer", "Fall", "Winter"]
In my viewDidLoad method i have this as follow:
itemPicker.hidden = true;
pickerTextfield.text = seasonalItems[0]
pickerTextfield.delegate = self
And the rest of the implementation:
// Below these lines is the implementation of the Picker
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return seasonalItems.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return seasonalItems[row]
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
pickerTextfield.text = seasonalItems[row]
itemPicker.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
itemPicker.hidden = false
return false
}
So the end result from this is when i tap the pickerTextfield object in the app, it shows the first item of the array (Spring) but in text within the UITextField object but it does not show the UIPickerView object with the other selectable items where i could select one and then hide it when selected.
My question is, where or what am i doing wrong here? i been trying to figure this out on my own but i do not seem to get good clear examples with Swift and storyboards. I much rather not drag a UIPickerView in the storyboard but rather the way i attempted to implement. Thanks
You can give UIPickerView as inputView for your TextField in which you want to show picker view.
You also do not need to initially hide picker view in this case.
pickerTextfield.inputView = itemPicker
When you use UIPickerView as inputView of any UITextField then when you tap on the TextField instead of default keypad PickerView will show.