neel
neel

Reputation: 209

How to get textfield values

I created a UI by parsing XML (I haven't used a xib file). That is, I created two text fields and one button. Now I want to get both textfield values when the button is clicked. Can anyone suggest how to do this?

Upvotes: 1

Views: 981

Answers (5)

anurag85
anurag85

Reputation: 11

You can access the text of a UITextField using

NSString *textFieldValue = myTextField.text;

or use

NSString *textFieldValue = [myTextField text]; 

Upvotes: 0

Saad Ur Rehman
Saad Ur Rehman

Reputation: 798

You have to declare UITextField other wise you cant access the textfields

Upvotes: 0

Dair
Dair

Reputation: 16240

Make sure your button has this:

[button addTarget:self action:@selector(getTextValue:) 
    forControlEvents:UIControlEventTouchUpInside];

Then, make this method (or whatever is in your @selector):

-(void)getTextValue:(id)sender {
    NSString *one = yourtextfield.text;
    NSString *two = yourtextfield2.text;
}

Upvotes: 1

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31730

Use the text property of UITextField , like below

NSString* myFirstValue = [myFirstTextField.text retain];

NSString* mySecondValue = [mySecondTextField.text retain];

after using both value don't forget to release them

 [myFirstValue release];
 [mySecondValue release];

Upvotes: 0

saadnib
saadnib

Reputation: 11145

if you have created textfield by code and you want to get text of textfield in button action then you can declare textfield in .h file and in your action call

[textfield text];

Upvotes: 0

Related Questions