Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView - retrieve textfield value from textfield added via code

Tags:

xcode

iphone

Here is the code I have to create an UIAlertView with a textbox.

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter A Username Here"     message:@"this gets covered!" 
                                               delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"OK!", nil];   
    UITextField *myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];

    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0, 60);
    [alert setTransform:myTransform];
    alert.tag = kAlertSaveScore;

    [myTextField setBackgroundColor:[UIColor whiteColor]];
    [alert addSubview:myTextField];
    [alert show];
    [alert release];
    [myTextField release];  

My question is, how do I get the value from the textfield in:

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

}

I know I can get the standard stuff for the alertview such as actionSheet.tag and such, but how would I get the above created textfield?

like image 938
George Avatar asked Dec 18 '22 02:12

George


1 Answers

@interface MyClass {
    UITextField *alertTextField;
}

@end

And instead of declaring it locally, just use that.

    //...
    alertTextField = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
    //...

- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *text = alertTextField.text;
    alertTextField = nil;
}
like image 195
Ed Marty Avatar answered May 03 '23 23:05

Ed Marty