ios - 在 UIAlertView 中尝试查看两个文本字段。它显示了 iOS7 文本字段数组的错误边界

标签 ios objective-c ios7 uialertview

您好,我正在尝试在 UIAlertView 中查看两个文本字段,但显示错误。我使用下面的代码来查看文本字段。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hello!"
                                                     message:@"Please enter your details:"
                                                    delegate:self
                                           cancelButtonTitle:@"Continue"
                                           otherButtonTitles:nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;

    UITextField * name = [alert textFieldAtIndex:0];
    name.keyboardType = UIKeyboardTypeAlphabet;
    name.placeholder = @"Enter your name";

    UITextField * phone = [alert textFieldAtIndex:1];
    phone.keyboardType = UIKeyboardTypeNumberPad;
    phone.placeholder = @"Enter your phone";

    [alert show];
    [alert release];
}

我已经使用上面的代码来查看两个文本文件,但我越来越喜欢。 “textFieldIndex (1) 超出文本字段数组的范围”

请告诉我上面代码中哪里做错了,并请告诉我如何解决此问题。

最佳答案

参见UIAlertView.h文件

/* Retrieve a text field at an index - raises NSRangeException when textFieldIndex is out-of-bounds. The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field. */

- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);

这样做:

alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;

UITextField * name = [alert textFieldAtIndex:0];
name.keyboardType = UIKeyboardTypeAlphabet;
name.placeholder = @"Enter your name";

UITextField * phone = [alert textFieldAtIndex:1];
phone.secureTextEntry = NO;
phone.keyboardType = UIKeyboardTypeNumberPad;
phone.placeholder = @"Enter your phone";

关于ios - 在 UIAlertView 中尝试查看两个文本字段。它显示了 iOS7 文本字段数组的错误边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23193317/

相关文章:

ios - Sprite Kit 中的 SKSpinLockSync 是什么以及如何修复它

ios - Firebase InvalidPathValidation hasChild 在 Swift 中

iphone - 如何将注释标题传递给另一个 View Controller

iphone - Core Location 无法在 ios 模拟器上运行

c - Obj-C 中的异常处理

iphone - 将 iOS 应用程序升级到更新的操作系统?

ios7 - 在iOS7上应该在应该开始和处理程序之间重置摇动手势转换(状态开始)

ios - .addTarget 方法将操作添加到 UITextView

ios - 我如何在中央 Controller 上显示一个具有透明度的抽屉导航?

ios - 在 uibutton SWIFT 周围包装文本