是否可以使用 3 个文本字段制作 UIAlertView?

Posted

技术标签:

【中文标题】是否可以使用 3 个文本字段制作 UIAlertView?【英文标题】:Is it possible to make an UIAlertView with 3 textfields? 【发布时间】:2013-01-04 19:47:05 【问题描述】:

当用户将联系人添加到他的地址簿时,我需要输入名字、中间名和姓氏。

附: 我终于找到了控制权,它允许:https://github.com/eaigner/CODialog

【问题讨论】:

为什么它必须是一个 AlertView?显示为 popOver 的自定义视图可能是更好的选择。 @vikingosegundo popover 只是在 iPad 上的一个选项 对不起,我的意思是模态视图。 github 上也有用于 iPhone 的弹出框,顺便说一句。我想要 UIAlertView 因为它的代码更少(我不需要再创建一个控制器)并且屏幕上的空间更少。 如果你想要更少的代码,你应该搜索使用块而不是委托的 uialertview 替换。 【参考方案1】:

你能推荐一些我可以使用的东西吗?

我将通过presentViewController:animated:completion: (ios 5+) 或presentModalViewController:animated: ios <5 (deprecated) 呈现模态视图

如果您想坚持使用 alertview,您可以在 cocoacontrols.com 上找到替代品。


From the docs:

子类化注释 UIAlertView 类旨在按原样使用,不支持子类化。此视图层次结构 类是私有的,不能修改。

添加文本视图会修改视图层次结构,并可能导致应用商店提交被拒绝。


使用this category,您可以轻松检查任何视图的视图层次结构。 (或在调试器控制台上使用po [alertView recursiveDescription]

注意:这是绝对不能在实际应用程序中使用的代码。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title"
                                                        message:@"msg"
                                                       delegate:nil
                                              cancelButtonTitle:@"ok"
                                              otherButtonTitles: nil];

    UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
    [textFiled setText:@"dont try that at home"];
    [alertView addSubview:textFiled];
    [alertView show];
    [alertView printSubviewsWithIndentation:4];

我们将记录这个层次结构

[0]: class: 'UIImageView'
[1]: class: 'UILabel'
[2]: class: 'UILabel'
[3]: class: 'UIAlertButton'
   [0]: class: 'UIImageView'
   [1]: class: 'UIButtonLabel'
[4]: class: 'UITextField'

导致这个

textView 只是放置在所有其他视图之上。实际上它必须放在[2]: class: 'UILabel' 下。我们可以通过摆弄视图层次结构(循环并重新排列它)或通过子类化 UIAlertView 并覆盖layoutSubviews 来做到这一点。这两件事,苹果都不想要。

所以总结一下,如果涉及到 UIAlertView,你有 3 个选择:

    照原样接受它(请记住,某些形式的输入可通过预定义的样式获得) 使用其他视图控制器来呈现模态视图 使用替代品。但不是 UIAlertView 的子类。


但是,如果有人仍然相信弄乱视图层次结构一个好主意并且比我和苹果的工程师更了解,这里是代码

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title"
                                                    message:@"Please read carefully the next 3 lines"
                                                   delegate:nil
                                          cancelButtonTitle:@"ok"
                                          otherButtonTitles: nil];

[alertView show];


CGFloat height = 25.0;

UILabel *msgLabel = [[alertView subviews] objectAtIndex:2];

UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(msgLabel.frame.origin.x, msgLabel.frame.origin.y+msgLabel.frame.size.height, msgLabel.frame.size.width, height)];
[textField1 setText:@"dont try that at home"];
[textField1 setBackgroundColor:[UIColor whiteColor]];
UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectOffset(textField1.frame, 0, height + 4)];
[textField2 setText:@"REALLY! dont try that at home"];
[textField2 setBackgroundColor:[UIColor whiteColor]];

UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectOffset(textField2.frame, 0, height + 4)];
[textField3 setText:@"REALLY! dont try that at home"];
[textField3 setBackgroundColor:[UIColor whiteColor]];

NSArray *followringSubviews = [[alertView subviews] subarrayWithRange:NSMakeRange(3, [[alertView subviews] count] - 3)];
[followringSubviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) 
    view.frame = CGRectOffset(view.frame, 0, 3*height);

];
[alertView addSubview:textField1];
[alertView addSubview:textField2];
[alertView addSubview:textField3];

alertView.frame = CGRectUnion(alertView.frame, CGRectOffset(alertView.frame, 0, 80));

结果:

【讨论】:

我会做的。但目前我不明白为什么 UIAlertView 框架没有改变,即使我这样做 [[UIAlertView alloc]initWithFrame:CGRectMake(12, 90, 260, 50)]; 正如我之前所说:Apple 不希望你改变外观。也许您可以通过子类化 UIAlertView 并覆盖 layoutSubviews 来解决它 调用show后必须设置新的框架。请参阅我的 不要在家尝试 示例。 @vikingosegundo 布局子视图似乎没有在 iOS7 上为 UIAlertView 调用:( @yonel,你读过答案了吗:注意:这是永远不应该在现实世界的应用程序中使用的代码。 是的:在 iOS 7 中这最终是不可能的,因为它总是被文档禁止。【参考方案2】:

你希望你可以这样做。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"msg" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
txtField.text=@"anoop here";
[alertView addSubview:txtField];
[alertView show];

编辑:

即使是自定义视图也可以完成您的工作。 (正如您在我之前的编辑中看到的那样。)

使用自定义形状大小、颜色、文本大小等创建自定义视图,然后将其显示为模型窗口。

编辑 2:

以上代码在 iOS7 上无法运行。

【讨论】:

好吧,在这种情况下我看到了空的 UIAlertView 你在txtField中添加了什么吗? 空/全文字段的结果是一样的 来自developer.apple.com/library/ios/#documentation/UIKit/Reference/… 子类化注释 UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。 @Flink — 使用替代品。 Annop 的回答是修改视图层次结构并可能导致 Appstore 拒绝。【参考方案3】:

使用附件视图属性作为警报视图

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"change Password " message:@"" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
     CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, 300);
     UIView *view = [[UIView alloc] initWithFrame:frame];

    UITextField* text1 = [[UITextField alloc] initWithFrame: CGRectMake(10, 10,self.view.frame.size.width-125, 30)];
    text1.backgroundColor=[UIColor whiteColor];
    UITextField* text2 = [[UITextField alloc] initWithFrame: CGRectMake(10, 45, self.view.frame.size.width-125, 30)];
    text2.backgroundColor=[UIColor whiteColor];
    UITextField* text3 = [[UITextField alloc] initWithFrame: CGRectMake(10, 80, self.view.frame.size.width-125, 30)];
    text3.backgroundColor=[UIColor whiteColor];
    text1.layer.borderColor=[UIColor lightGrayColor].CGColor;
    text1.layer.borderWidth=1;
    text2.layer.borderColor=[UIColor lightGrayColor].CGColor;
    text2.layer.borderWidth=1;
    text3.layer.borderColor=[UIColor lightGrayColor].CGColor;
    text3.layer.borderWidth=1;
    text1.placeholder=@"  Enter  old password ";
   [view addSubview:text1];
   [view addSubview:text2];
   [view addSubview:text3];
    [alertView setValue:view forKey:@"accessoryView"];
    view.backgroundColor = [UIColor whiteColor];
    [alertView show];

【讨论】:

以上是关于是否可以使用 3 个文本字段制作 UIAlertView?的主要内容,如果未能解决你的问题,请参考以下文章

制作删除按钮以删除 inputtext 字段中的文本

ios中的用户注释

使用 Fabric.js 的交互式文本字段

是否可以在 PyQt/PySide2 中为 QLineEdit 的文本制作“破碎”边框

如何制作 if 语句来检查所有或仅一个文本字段是不是为空

如何使用正则表达式匹配四个数字字段或文本字符串