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

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

本文介绍了UIAlertView-从通过代码添加的文本字段中检索文本字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我创建带有文本框的UIAlertView的代码.

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 {

}

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

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?

推荐答案

@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;
}

这篇关于UIAlertView-从通过代码添加的文本字段中检索文本字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 21:02