当用户将联系人添加到他的地址簿时,我需要输入名字、中间名和姓氏。
PS: 最后我找到了 control,它允许:https ://github.com/eaigner/CODialog
当用户将联系人添加到他的地址簿时,我需要输入名字、中间名和姓氏。
PS: 最后我找到了 control,它允许:https ://github.com/eaigner/CODialog
你能推荐一些我可以用的东西吗?
我会通过presentViewController:animated:completion:
(ios 5+)或presentModalViewController:animated:
ios <5 (deprecated)呈现模态视图
如果你想坚持使用 alertview,你可以在cocoacontrols.com上找到替代品。
子类化注释
UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。
添加文本视图正在修改视图层次结构,并可能导致应用商店提交被拒绝。
使用此类别,您可以轻松检查任何视图的视图层次结构。(或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 *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 *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 *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];