【问题标题】:iOS multiple text fields on a UIAlertview In IOS 7iOS 7中的UIAlertview上的iOS多个文本字段
【发布时间】:2013-09-24 00:40:53
【问题描述】:

所以新的 iOS 7 已经问世,我正在尝试向 UIAlertviews 添加多个文本字段和标签。我需要三个。我一直在尝试将它们添加为子视图,但这不再起作用。我还尝试使用 UIAlertViewStylePlainTextInput 添加多行,但它似乎只返回一个文本字段。

我还需要添加标签以向他们展示要输入的内容。有没有办法在新的 iOS 7 中完成这项任务?

【问题讨论】:

  • 请出示您的代码。该代码以前在 iOS6 中是否有效?
  • 您不能将子视图添加到 UIAlertView。这不是它的目的。您可以使用对 1 个或 2 个文本字段的标准支持。不要添加标签,而是设置文本字段的 prompt 属性。
  • 好的,谢谢,我看到你可以在 iOS 5 上做到这一点。我猜他们在 iOS 7 上把它拿出来了。我会制作另一个视图并将其添加为子视图,谢谢你的帮助。

标签: ios uitextfield uialertview textfield


【解决方案1】:

我发现在 iOS7 中使用带有多个文本字段的 UIAlertView 的唯一解决方案是仅用于登录。

使用这一行来初始化你的 alertView

[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];

这是为了获取用户的输入:

user = [alert textFieldAtIndex:0].text;
pw = [alert textFieldAtIndex:1].text

出于登录以外的其他目的,请在以下位置查看其他主题:UIAlertView addSubview in iOS7

【讨论】:

  • 是的,感谢您的回答我最终制作了一个看起来像 uiAlertView 的自定义子视图。
  • 谢谢,这个答案很好,也指出了 UIAlertView 是多么无用......来自Apple的信息很明确:去写代码。这是你能做的最有成效的事情。
【解决方案2】:

您可以在 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];

【讨论】:

    【解决方案3】:

    如果您只想在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;
    

    【讨论】:

    • 感谢您添加有关 secureEntry 和占位符文本的详细信息。正因为如此,更好的答案:-)
    • 这在 ios 11 中不起作用。有没有人可以提出建议。
    猜你喜欢
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多