Create a text field in UIAlertView with obj-c

116 views Asked by At

How to put a text field into a UIAlertView? And how to get the value of the string that the user just inputted in the text field? All with Objective-C

Thanks very much.

1

There are 1 answers

0
Claudio Castro On BEST ANSWER
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated{
    [self alert];
}

- (void) alert {
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"Name"
                                                                                  message: @"Input your new username"
                                                                              preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"username";
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSArray * textfields = alertController.textFields;
        UITextField * usernamefield = textfields[0];
        NSLog(@"%@",usernamefield.text);

    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

@end