iphone – UIAlertView – 通过代码添加从文本字段中检索文本字段值

前端之家收集整理的这篇文章主要介绍了iphone – UIAlertView – 通过代码添加从文本字段中检索文本字段值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我用文本框创建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,25)];
    //...

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

猜你在找的Xcode相关文章