UITertController与UITextfield不显示标题和消息

请帮助我解决以下问题。

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

我使用上面的代码,但它显示我的结果在下面的截图。 无法显示标题和消息
在此处输入图像描述

提前致谢 !

这是创建UIAlertController以显示消息和标题的代码。

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

在此处输入图像描述

对于swift,你可以像下面的代码一样做:

 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) 

在此处输入图像描述

从Github下载演示: https : //github.com/nitingohel/NGAlertViewController-Swift2.0

我测试了你在这里发布的代码,并且在所有模拟器和iOS 8.0+中的设备都可以正常工作或者如果你想在ios 7.0中测试它,那么你必须使用UIAlertview。

代码是: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 

输出捕捉:

在此处输入图像描述

坦率地说,你的代码工作正常。 只是预感,尝试添加[self presentViewController:alertController animated:YES completion:nil]; 主队列上的这段代码。

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

我修改了你的代码,它对我有用。 基本上,我使用“show”控制器而不是“present”。

这是剪辑:

 -(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] } 

试试这个代码吧。

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