所以新的 iOS 7 已经问世,我正在尝试向 UIAlertviews 添加多个文本字段和标签。我需要三个。我一直在尝试将它们添加为子视图,但这不再起作用。我还尝试使用 UIAlertViewStylePlainTextInput 添加多行,但它似乎只返回一个文本字段。
我需要添加标签以向他们展示要输入的内容。有没有办法用新的 iOS 7 完成这项任务?
所以新的 iOS 7 已经问世,我正在尝试向 UIAlertviews 添加多个文本字段和标签。我需要三个。我一直在尝试将它们添加为子视图,但这不再起作用。我还尝试使用 UIAlertViewStylePlainTextInput 添加多行,但它似乎只返回一个文本字段。
我需要添加标签以向他们展示要输入的内容。有没有办法用新的 iOS 7 完成这项任务?
我发现在 iOS7 中使用带有多个文本字段的 UIAlertView 的唯一解决方案仅用于登录。
使用这一行来初始化你的 alertView
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
这可以获取用户的输入:
user = [alert textFieldAtIndex:0].text;
pw = [alert textFieldAtIndex:1].text
除了登录查看其他线程之外的其他目的:iOS7 中的 UIAlertView addSubview
您可以在 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;