Getting Text from UITextField

Discussion in 'iPhone/iPad Programming' started by matthew858, Apr 16, 2009.

  1. macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #1
    I have 2 UITextFields inside a UIAlertView like so:
    Code:
    	
    	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?
     
  2. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #2
    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).
     
  3. thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #3
    Can:
    Code:
    - (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.
     
  4. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #4
    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.
     
  5. thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #5
    That's the only alert view that I have in my app. So, can I use:

    Code:
    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?
     
  6. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #6
    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.
     
  7. thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #7
    The code used is the code in the very first post plus:
    Code:
    - (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.
     
  8. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #8
    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?
     

Share This Page