How to get text from UITextField in UIAlertView

I am trying to add a new cell to a tableview and raise a warning using a UITextField to allow the user to enter the title that they want to pass to the new cell. I have code to display a warning using UITextField when the + button is pressed, and code to add a new cell, however, I don’t know how to get text from UITextField to insert it into the cell header.

This is my code to display a warning:

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:

    - (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 the "Ok" button is clicked and add it to mutablearray named eventList? Thank!

+3
source share
4

eventNameField -

eventNameField.tag = 1001;

alertViewDelegate TextField - [UIView viewWithTag:]

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

eventNameField.text

//declare the array
NSMutableArray* eventList = [NSMutableArray array];

//set its value
[eventList addObject:eventNameField.text];
+1

DHamrick, , , , .

subviews alertView [alertView subviews] ( NSCFArray), , UITextField, :

[[[alertView subviews] objectAtIndex:4] text]
0

UIAlertView.

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

alertTextField :

@propery (nonatomic, strong) UITextField *alertTextField;

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

alertView , @property.

0
source

Source: https://habr.com/ru/post/1772718/


All Articles