加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 18:12:31 所属栏目:百科 来源:网络整理
导读:这是我用文本框创建UIAlertView的代码. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Enter A Username Here" message:@"this gets covered!" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:@"OK!",nil]; UITextField *myTe
这是我用文本框创建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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读