UITertController与UITextfield不显示标题和消息 [英] UIAlertController with UITextfield does not display Title and Message
本文介绍了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];
我使用了上面的代码,但它在下面的截图中显示了我的结果。无法显示标题和消息
I have using above code but it shows me result in below screenshot. Not able to display title and message
提前致谢!
推荐答案
这里是创建 UIAlertController
的代码,用于显示消息和标题。
Here it is the code for create UIAlertController
for showing Message and title.
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,你可以做同样的事情,如下面的代码:
For swift you can do same thing like 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)
从Github下载演示: https://github.com/nitingohel/NGAlertViewController-Swift2.0
Download Demo from Github: https://github.com/nitingohel/NGAlertViewController-Swift2.0
这篇关于UITertController与UITextfield不显示标题和消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文