[英]How do I get text from UITextField in an UIAlertView
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. 我正在尝试向tableview添加一个新单元格,并使用UITextField弹出一个警报,以允许用户输入他们希望为新单元格提供的标题。 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. 我有代码在按下“+”按钮时用UITextField弹出警报,以及添加新单元格的代码,但是我不知道如何从UITextField获取文本以将其插入到单元格的标题中。
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];
and my alertView action is: 我的alertView操作是:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:@"Cancel"]) {
return;
}
else if([buttonTitle isEqualToString:@"Ok"]) {
}
}
What can I do to get the text from eventNameField when "Ok" is pressed and add it to a mutablearray named eventList? 当按下“确定”时,如何从eventNameField获取文本并将其添加到名为eventList的mutablearray中? Thanks! 谢谢!
Set the tag on eventNameField to something meaningful 将eventNameField上的标记设置为有意义的内容
eventNameField.tag = 1001;
Then inside of the alertViewDelegate you can get the TextField by using - [UIView viewWithTag:] 然后在alertViewDelegate里面你可以使用 - [UIView viewWithTag:]来获取TextField
UITextField* textField = (UITextField*)[alertView viewWithTag:1001];
eventNameField.text
should give you the value eventNameField.text
应该为您提供值
//declare the array
NSMutableArray* eventList = [NSMutableArray array];
//set its value
[eventList addObject:eventNameField.text];
The old answers don't take advantage of changes in UIAlertView. 旧的答案没有利用UIAlertView的变化。
In iOS 5 and beyond, there is an easier way of using UITextfield on an alert view: 在iOS 5及更高版本中,有一种在警报视图上使用UITextfield的简便方法:
UIAlertView *alertPopUp = [[UIAlertView alloc] initWithTitle:@"Enter event title" message:@"" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alertPopUp.alertViewStyle = UIAlertViewStylePlainTextInput;
self.alertTextField = [message textFieldAtIndex:0];
self.alertTextField.keyboardType = UIKeyboardTypeAlphabet;
alertPopUp.delegate = self;
[alertPopUp show];
[self.alertTextField becomeFirstResponder];
where alertTextField was set up like this: alertTextField的设置方式如下:
@propery (nonatomic, strong) UITextField *alertTextField;
Then you can access alertTextField in your delegate response: 然后,您可以在委托响应中访问alertTextField:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:@"Cancel"]) {
return;
}
else if([buttonTitle isEqualToString:@"Ok"]) {
NSLog(@"your text string is %@", self.alertTextField.text);
}
}
You could also just give the alertView a unique tag, and compare tag numbers, rather than save a reference to it using @property. 您还可以给alertView一个唯一的标记,并比较标记号,而不是使用@property保存对它的引用。
DHamrick probably has the better solution, but found one more that could work. DHamrick可能有更好的解决方案,但又找到了一个可行的解决方案。
You can get all the subviews inside alertView with [alertView subviews] (returns a NSCFArray), then you just have to find the one that is of class UITextField, in your case you could get it with: 您可以使用[alertView子视图]获取alertView内的所有子视图(返回NSCFArray),然后您只需要找到UITextField类的子视图,在您的情况下,您可以使用:
[[[alertView subviews] objectAtIndex:4] text]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.