0

实现了以下代码,但我收到错误“原因:'文本字段索引 (0) 超出了文本字段数组的范围'”

当弹出功能被触发时,我会弹出

- (IBAction)popup:(id)sender{

    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Post your message" message:@"\n\n\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
    UITextField *textField;
    textField = [[UITextField alloc] init];
    [textField setBackgroundColor:[UIColor whiteColor]];
    textField.delegate = self;
    textField.borderStyle = UITextBorderStyleLine;
    textField.frame = CGRectMake(15, 75, 255, 30);
    textField.font = [UIFont fontWithName:@"ArialMT" size:20];
    textField.placeholder = @"Post your message";
    textField.textAlignment = UITextAlignmentCenter;
    textField.keyboardAppearance = UIKeyboardAppearanceAlert;
    [textField becomeFirstResponder];
    [alert addSubview:textField];
    [alert show];


    //NSLog(@"%@",buttonIndex)

}

在此处输入图像描述

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    NSString *value=[[alertView textFieldAtIndex:0]text];
    if (buttonIndex == 1) {
        if ([[alertView textFieldAtIndex:0].text length] > 0 ||
            [alertView textFieldAtIndex:0].text != nil ||
            [[alertView textFieldAtIndex:0].text isEqual:@""] == FALSE)
        {
           NSLog(@" textbox value here => %@",value);
        }
        else
        {
             NSLog(@" text box was empty ");
        }
    } else {
        NSLog(@" Cancel is pressed");
    }
}
4

2 回答 2

3

问题就在此时[alertView textFieldAtIndex:0]。您的 textField 不在第 0 位。如果您NSLog(@"%@",[alertView subviews]);可以看到您的文本字段最后在数组中而不是在第一个位置。所以尝试以这种方式获取文本字段。

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

    if (buttonIndex == 1) {
        NSLog(@"%@",[alertView subviews]);
        UITextField *txtField = [[alertView subviews] lastObject];
        if ([txtField.text length] > 0 ||
            txtField.text != nil ||
            [txtField.text isEqual:@""] == FALSE)
        {
            NSLog(@" textbox value here => %@",txtField.text);
        }
        else
        {
            NSLog(@" text box was empty ");
        }
    }
    else
    {
        NSLog(@" Cancel is pressed");
    }
}
于 2013-03-28T10:43:58.573 回答
0

首先在创建后在代码中添加这一行UIAlertView

[alert setAlertViewStyle:UIAlertViewStylePlainTextInput];// you can use also another style like... `UIAlertViewStyleSecureTextInput` or `UIAlertViewStyleLoginAndPasswordInput`

然后试试下面的逻辑..

//your other code
UITextField *textField = [[UITextField alloc] init];
textField.tag = 111; // just add this line...
//your other code..

然后只需替换以下方法...

- (void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   UITextField* myField = (UITextField*)[alertView viewWithTag:111];
   NSString *value = myField.text;
   if (buttonIndex == 1) {
      if ([[alertView textFieldAtIndex:0].text length] > 0 ||
        [alertView textFieldAtIndex:0].text != nil ||
        [[alertView textFieldAtIndex:0].text isEqual:@""] == FALSE)
      {
           NSLog(@" textbox value here => %@",value);
      }
      else
      {
           NSLog(@" text box was empty ");
      }
    } else {
       NSLog(@" Cancel is pressed");
    }
}
于 2013-03-28T10:40:12.790 回答