是否可以制作带有3个文本字段的UIAlertView? [英] Is it possible to make an UIAlertView with 3 textfields?

查看:141
本文介绍了是否可以制作带有3个文本字段的UIAlertView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to input first, middle and last names when user will add a contact to his addressbook.

PS:
最后我发现控制,允许: https://github.com/eaigner/CODialog

推荐答案


你能建议我可以使用的东西吗?

Can you suggest something I can use instead?

我通过 presentViewController:animated:completion:(ios 5 +) presentModalViewController:animated: ios< 5(已弃用)

I'd present a modal view via presentViewController:animated:completion: (ios 5+) or presentModalViewController:animated: ios <5 (deprecated)

如果您想坚持使用alertview,可以在 cocoacontrols.com

If you want to stick with an alertview, you can find replacements on cocoacontrols.com.

来自文档:


子类注释

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

Subclassing Notes
The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

添加文本视图正在修改视图层次结构和可能导致appstore提交拒绝。

adding text views is modifying the view hierarchy and could lead to appstore submission refusal.

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

using this category you can easily inspect the view hierarchy of any view. (or use po [alertView recursiveDescription] on the debugger console)

注意:这是代码不应该在现实世界的应用程序中使用。

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 来实现。这两件事,苹果都不想要。

The textView is just placed over all others. actually it must be placed under [2]: class: 'UILabel'. We could do this via fiddling with the view hierarchy (loop through it and re-arrange this) or by sublassing UIAlertView and overwriting layoutSubviews. Both things, apple does not want.

总而言之,如果涉及到UIAlertView,你有3种选择:

So to summarize it, if it comes to UIAlertView, you have 3 options:


  1. 与它一起生活(记住,某些形式的输入可通过预定义的样式获得)

  2. 使用其他视图控制器来呈现模态视图

  3. 使用替换。但是没有UIAlertView的子类。









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



But if someone is still convinced that messing around with the view hierarchy IS a good idea and knows better than me and apple's engineers, here is the code

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

结果:

这篇关于是否可以制作带有3个文本字段的UIAlertView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆