简体   繁体   中英

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. 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];

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"]) {

 }

}

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

Set the tag on eventNameField to something meaningful

eventNameField.tag = 1001;

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

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

eventNameField.text should give you the value

//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.

In iOS 5 and beyond, there is an easier way of using UITextfield on an alert view:

   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:

@propery (nonatomic, strong) UITextField *alertTextField;

Then you can access alertTextField in your delegate response:

- (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.

DHamrick probably has the better solution, but found one more that could work.

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 subviews] objectAtIndex:4] text]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM