PDA

View Full Version : Getting Text from UITextField




matthew858
Apr 17, 2009, 01:25 AM
I have 2 UITextFields inside a UIAlertView like so:


UIAlertView *firstTimeWelcome = [[UIAlertView alloc]
initWithTitle: @"Welcome!"
message:@"Please enter your details:"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Continue", nil];
[firstTimeWelcome addTextFieldWithValue:@""label:@"Username"];
[firstTimeWelcome addTextFieldWithValue:@""label:@"Password"];


// Name field
UITextField *tf = [firstTimeWelcome textFieldAtIndex:0];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeAlphabet;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
tf.autocorrectionType = UITextAutocorrectionTypeNo;
username = tf.text;


//Password Field
tf = [firstTimeWelcome textFieldAtIndex:1];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeAlphabet;
tf.secureTextEntry = YES;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
tf.autocorrectionType = UITextAutocorrectionTypeNo;
password = tf.text;
[firstTimeWelcome show];
[firstTimeWelcome release];


The issue that I am having is that I can't seem to get password or username (both NSStrings) to store the values of the text fields. What am I doing wrong?



caveman_uk
Apr 17, 2009, 02:31 AM
You're trying to access the values BEFORE you've shown the alert. You need to do it after the alert has been dismissed (this means in another method in the alertViews delegate - I can't remember which method it is that gets called - it's in the docs).

matthew858
Apr 17, 2009, 03:29 AM
Can:

- (void)alertView:(UIAlertView *)actionSheet ClickedButtonAtIndex:(NSInteger)buttonIndex {


be used to grab the contents of the text boxes, or is there another method? I am currently using the above code to link the button press to another action.

caveman_uk
Apr 17, 2009, 03:52 AM
That's the one although obviously if you have more than one alertView you'll need to check which one is responsible for getting the method called.

matthew858
Apr 17, 2009, 04:18 AM
That's the only alert view that I have in my app. So, can I use:


username = tf.text;
password = tf.text;


within the code I posted in my 2nd reply above and have it work?

Edit: What I had to do was declare the text view in the delegate, but it is still not passing the value of the text field. Any help?

caveman_uk
Apr 17, 2009, 05:00 AM
How are you accessing the textfields in the clickedButton method ?

Incidently I noticed you had the message as having

ClickedButtonAtIndex

it should be

clickedButtonAtIndex

If this is wrong in your code the method won't get called.

matthew858
Apr 17, 2009, 05:22 AM
The code used is the code in the very first post plus:

- (void)alertView:(UIAlertView *)firstTimeWelcome clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
password = tf.text;
[self sendToServer];
}
}


sendToServer is a custom function that is to do with push notification, and unrelated.

caveman_uk
Apr 17, 2009, 05:49 AM
Is tf now an instance variable? Previously it was only in scope in the method where you created the alertview.

Can you put an NSLog debug line in the clickedButton...method to see if it's actually being called?