UIAlertController with UITextfield does not display header and message

Please help me with the problem below.

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"What was the value collected?" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.keyboardType = UIKeyboardTypeNumberPad; textField.placeholder = @"What was the value collected?"; }]; [alertController addAction:[UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UITextField *txtValue = alertController.textFields.firstObject; toCollect = [txtValue.text floatValue]; }]]; [self presentViewController:alertController animated:YES completion:nil]; 

I have the code above, but it shows me the result below the screenshot. Cannot display the title and message.
enter image description here

Thanks in advance!

+5
source share
5 answers

Here is the code to create the UIAlertController to display the message and the header.

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Controller" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert]; UIViewController *viewController = [[UIViewController alloc]init]; [viewController.view setBackgroundColor:[UIColor blueColor]]; UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, 250, 30)]; lbl.text = @"This is a label"; lbl.textAlignment = NSTextAlignmentCenter; lbl.textColor = [UIColor whiteColor]; [viewController.view addSubview:lbl]; UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 35, 250, 30)]; tf.borderStyle = UITextBorderStyleRoundedRect; tf.placeholder = @"Enter your name"; [viewController.view addSubview:tf]; [alertController setValue:viewController forKey:@"contentViewController"]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"Cancel action"); }]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"OK action"); NSLog(@"Text Value : %@",tf.text); }]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:alertController animated:YES completion:nil]; }); 

enter image description here

For quick, you can do the same as the following code:

 let alert = UIAlertController(title: "Alert Controller", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert) let cancelAction = UIAlertAction( title: "Cancel", style: UIAlertActionStyle.Destructive) { (action) in } let confirmAction = UIAlertAction( title: "OK", style: UIAlertActionStyle.Default) { (action) in } alert.addAction(confirmAction) alert.addAction(cancelAction) let VC = UIViewController() VC.view.backgroundColor = UIColor.blackColor() let lbl = UILabel() lbl.frame = CGRectMake(10, 8, 250, 30) lbl.text = "this is a label" lbl.textAlignment = NSTextAlignment.Center lbl.textColor = UIColor.whiteColor() VC.view .addSubview(lbl) let txt = UITextField() txt.frame = CGRectMake(10, 35, 250, 30) txt.borderStyle = UITextBorderStyle.RoundedRect txt.placeholder = "enter text" VC.view .addSubview(txt) alert.setValue(VC, forKey: "contentViewController") self.presentViewController(alert, animated: true, completion: nil) 

enter image description here

Download demo from Github: https://github.com/nitingohel/NGAlertViewController-Swift2.0

+8
source

I tested your code, which you posted here, and it works great on all simulators, as well as on devices in iOS 8.0+ or, if you want to test it in ios 7.0, then you need to use UIAlertview.

Code: view controller.m

 @interface ViewController () { float toCollect; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } -(IBAction)btntapped:(id)sender { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"What was the value collected?" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.keyboardType = UIKeyboardTypeNumberPad; textField.placeholder = @"What was the value collected?"; }]; [alertController addAction:[UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UITextField *txtValue = alertController.textFields.firstObject; toCollect = [txtValue.text floatValue]; }]]; [self presentViewController:alertController animated:YES completion:nil]; } @end 

Output Captures:

enter image description here

+2
source

To be honest, your code is working fine. Just guess, try adding [self presentViewController:alertController animated:YES completion:nil]; this piece of code is in the main queue.

 dispatch_async(dispatch_get_main_queue(), ^{ [self presentViewController:alertController animated:YES completion:nil]; }); 
0
source

I changed your code a bit and it worked for me. Basically, I used the "show" controller instead of the "present".

Here is a snip:

 -(IBAction)pressMe:(id)sender { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"What was the value collected?" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.keyboardType = UIKeyboardTypeNumberPad; textField.placeholder = @"What was the value collected?"; }]; [alertController addAction:[UIAlertAction actionWithTitle:@"Submit" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UITextField *txtValue = alertController.textFields.firstObject; NSString *string = txtValue.text; NSLog(@"String: %@", string); }]]; [self showViewController:alertController sender:nil] } 
0
source

Try this code for this.

 if ([UIAlertController class]) { // use UIAlertController UIAlertController *alert= [UIAlertController alertControllerWithTitle:@"Enter Folder Name" message:@"Keep it short and sweet" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){ //Do Some action here UITextField *textField = alert.textFields[0]; NSLog(@"text was %@", textField.text); }]; UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSLog(@"cancel btn"); [alert dismissViewControllerAnimated:YES completion:nil]; }]; [alert addAction:ok]; [alert addAction:cancel]; [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"folder name"; textField.keyboardType = UIKeyboardTypeDefault; }]; [self presentViewController:alert animated:YES completion:nil]; } 
0
source

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


All Articles