新的 iOS 7 已经出来了,我正在尝试向 UIAlertviews 添加多个文本字段和标签。我需要三个。我一直在尝试将它们添加为子视图,但这不再起作用。我还尝试使用 UIAlertViewStylePlainTextInput 添加多行,但它似乎只返回一个文本字段。
我需要添加标签来向他们展示要输入的内容。有没有办法用新的 iOS 7 来完成这个任务?
我发现在 iOS7 中使用带有多个文本字段的 UIAlertView 的唯一解决方案是仅用于登录。
使用这一行来初始化你的alertView
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
这是为了获取用户输入:
user = [alert textFieldAtIndex:0].text;
pw = [alert textFieldAtIndex:1].text
出于登录以外的其他目的,请查看其他线程,如下所示:UIAlertView addSubview in iOS7
您可以在 iOS7 的标准警报视图中将 accessoryView 更改为任何自己的 customContentView
[alertView setValue:customContentView forKey:@"accessoryView"];
请注意,您必须在
[alertView show]之前调用此方法。
最简单的说明示例:
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
v.backgroundColor = [UIColor yellowColor];
[av setValue:v forKey:@"accessoryView"];
[av show];
UIAlertView
添加两个文本字段,您可以使用
UIAlertViewStyleLoginAndPasswordInput
并按如下方式修改文本字段:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Some Title" message:@"Some Message." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:@"No, thanks", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert textFieldAtIndex:1].secureTextEntry = NO; //Will disable secure text entry for second textfield.
[alert textFieldAtIndex:0].placeholder = @"First Placeholder"; //Will replace "Username"
[alert textFieldAtIndex:1].placeholder = @"Second Placeholder"; //Will replace "Password"
[alert show];
之后,在 UIAlertView 委托中,您可以简单地使用以下方法获取文本:
text1 = [alert textFieldAtIndex:0].text;
text2 = [alert textFieldAtIndex:1].text;