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

2024-01-05

这是我必须创建带有文本框的 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 {

}

我知道我可以获得警报视图的标准内容,例如 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UIAlertView - 从通过代码添加的文本字段检索文本字段值 的相关文章

随机推荐