Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIAlertView-从通过代码添加的文本字段中检索文本字段值_Iphone_Xcode - Fatal编程技术网

Iphone UIAlertView-从通过代码添加的文本字段中检索文本字段值

Iphone UIAlertView-从通过代码添加的文本字段中检索文本字段值,iphone,xcode,Iphone,Xcode,下面是我创建带有文本框的UIAlertView的代码 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter A Username Here" message:@"this gets covered!" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTi

下面是我创建带有文本框的UIAlertView的代码

    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];  
我的问题是,如何从中的文本字段中获取值:

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

}
我知道我可以获得alertview的标准内容,比如actionSheet.tag等等,但是我如何获得上面创建的文本字段呢

@interface MyClass {
    UITextField *alertTextField;
}

@end
与其在本地声明,不如使用它

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

- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *text = alertTextField.text;
    alertTextField = nil;
}

给它一个标签,然后用标签找到它。因此,使用您的代码:

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];
然后,在回调中,无论在何处,都不必担心文本字段的内存管理或状态管理:

- (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]);
}