tags:

views:

633

answers:

3

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?

Thanks in advance for any and all help.

Geo...

A: 

Just do:

 NSString *hereBeString = [myTextField text];

Put that before the

[myTextField release];

Then you can do whatever you want with the string.

This is shown working in my example here

Matt S.
That doesn't seem like it will work at all. First of all, it's an NSString which is immutable, second of all, the property is marked as `copy` which means you won't even be getting the original string if it were mutable.
Ed Marty
well I can tell you for a fact it works. I've used it probably 20 times in the past. If you want it a mutable string then just make it one. I just gave an example
Matt S.
+2  A: 
@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;
}
Ed Marty
+1  A: 

Just give it a tag, and find it using the tag later. So, using your code:

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;

// Give the text field some unique tag
[myTextField setTag:10250];

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

Then, in the call-back, wherever that happens to be and without having to worry about memory management or state management of the text field:

- (void) alertView:(UIAlertView *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
  // Get the field you added to the alert view earlier (you should also
  // probably validate that this field is there and that it is a UITextField but...)
  UITextField* myField = (UITextField*)[actionSheet viewWithTag:10250];
  NSLog(@"Entered text: %@", [myField text]);
}
Jason Coco