1

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") } } 
2
  • you can use .text method of UITextField. Commented Jun 14, 2017 at 9:51
  • if userGenderTextField.text == "Male" use this Commented Jun 14, 2017 at 9:52

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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] } 

Comments

-1

In if statement you put textfieldname.text== male print male else female

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.