gpt4 book ai didi

ios - 带有 UITextfield 的 UIAlertController 不显示标题和消息

转载 作者:可可西里 更新时间:2023-11-01 04:23:15 27 4
gpt4 key购买 nike

请帮我解决以下问题。

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

我使用了上面的代码,但它在下面的屏幕截图中显示了结果。无法显示标题和消息
enter image description here

提前致谢!

最佳答案

这是创建用于显示消息和标题的 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];
});

enter image description here

对于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)

enter image description here

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

关于ios - 带有 UITextfield 的 UIAlertController 不显示标题和消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32843826/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com