如何从UIAlertView中的UITextField获取文本 [英] How do I get text from UITextField in an UIAlertView

查看:46
本文介绍了如何从UIAlertView中的UITextField获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向表视图添加新单元格,并弹出带有UITextField的警报,以允许用户输入他们希望赋予新单元格的标题.我有按下"+"按钮时会弹出带有UITextField的警报的代码,以及添加新单元格的代码,但是我不知道如何从UITextField中获取文本以将其插入到单元格标题中

I'm trying to add a new cell to a tableview, and pop up an alert with a UITextField to allow the user to input the title they wish to give the new cell. I have code to pop up an alert with a UITextField when the "+" button is pressed, and the code to add a new cell, however I don't know how to get the text from the UITextField to insert it into the cell's title.

这是我弹出警报的代码:

This is my code to pop up the alert:

UIAlertView* alertPopUp = [[UIAlertView alloc] init];
 [alertPopUp setDelegate:self];
 [alertPopUp setTitle:@"Enter event title"];
 [alertPopUp setMessage:@" "];
 [alertPopUp addButtonWithTitle:@"Cancel"];
 [alertPopUp addButtonWithTitle:@"OK"];

 UITextField * eventNameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
 [eventNameField setBackgroundColor:[UIColor whiteColor]];
 [alertPopUp addSubview:eventNameField];
 [alertPopUp show];

而我的alertView动作是:

and my alertView action is:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
 NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
 if([buttonTitle isEqualToString:@"Cancel"]) {
  return;
 }
 else if([buttonTitle isEqualToString:@"Ok"]) {

 }

}

当按下"Ok"键并将其添加到名为eventList的可变数组中时,如何从eventNameField中获取文本?谢谢!

What can I do to get the text from eventNameField when "Ok" is pressed and add it to a mutablearray named eventList? Thanks!

推荐答案

将eventNameField上的标签设置为有意义的

Set the tag on eventNameField to something meaningful

eventNameField.tag = 1001;

然后在alertViewDelegate内部,您可以使用-[UIView viewWithTag:]

Then inside of the alertViewDelegate you can get the TextField by using - [UIView viewWithTag:]

UITextField* textField = (UITextField*)[alertView viewWithTag:1001];

这篇关于如何从UIAlertView中的UITextField获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆