1

我已经成功创建了两个带有四个按钮的警报。第一个 AlertView 效果很好,但第二个 alertView 让我崩溃了。这是我的代码:

-(void)showAlertWithTextField{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"info" message:@"Set Time For The Game." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
[alertView setTag:101];
alertView.alertViewStyle=UIAlertViewStylePlainTextInput;
[[alertView textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumberPad];
[[alertView textFieldAtIndex:0] becomeFirstResponder];
[alertView show];
}

-(void)showexitAlertView
{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"Timeout" message:[NSString stringWithFormat:@"Your final score is: %i", runningSore] delegate:self cancelButtonTitle:@"Restart" otherButtonTitles:@"Exit", nil];
[alertView setTag:102];
[alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITextField *field = [alertView textFieldAtIndex:0];
setTime = [field.text intValue];

NSLog(@"%d", setTime);
if (alertView.tag == 101 && buttonIndex == 0) {
    //if (buttonIndex == 0){
    NSLog(@"Ok");
    [self setTimer];
    [self countDownTimerPrepare];
    [self countDownTimerIntro];
    [self prepareForIntroAnimation];
    [self performIntroAnimation];
    [self categoriesList];
}
else if (buttonIndex == 1){
    NSLog(@"Cancel");
    ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:@"ChooseViewController" bundle:nil];
    //controller.countdownLabel.text= score;
    [self presentViewController:controller animated:YES completion:nil];
}
else if (alertView.tag==102 && buttonIndex == 0){


        NSLog(@"Restart");
    }
else if (buttonIndex == 1){
    NSLog(@"Exit");

}
}

这是我在第二个警报视图中按下按钮时的 drbugging 消息:

2013-09-11 17:12:06.106 Diplomatiki [2808:c07] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“textFieldIndex(0)超出了文本字段数组的范围”*第一次抛出调用stack: (0x1c42012 0x1737e7e 0x1c41deb 0xa7f0f0 0xb5d0 0xa72020 0x174b705 0x682920 0x6828b8 0x743671 0x743bcf 0x742d38 0x6b233f 0x6b2552 0x6903aa 0x681cf8 0x1fc2df9 0x1fc2ad0 0x1bb7bf5 0x1bb7962 0x1be8bb6 0x1be7f44 0x1be7e1b 0x1fc17e3 0x1fc1668 0x67f65c 0x29fd 0x2925 0x1) libc++abi.dylib: terminate called throwing an exception

我会感谢任何建议。先感谢您。

4

3 回答 3

1

Axel,与上一个案例相同的问题

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{

  if (alertView.tag == 101){
   NSString *inputText = [[alertView textFieldAtIndex:0] text];
    if( [inputText length] >= 1 ){
       return YES;
     }
     else{
       return NO;
     }
   }else{
      return NO;
     }
 }
于 2013-09-11T16:52:44.243 回答
1

崩溃是由于您的第二个警报视图没有文本字段。但是您正在尝试访问文本字段,并且clickedButtonAtIndex:无论单击哪个警报视图,它的值都是如此。

像这样实现:clickedButtonAtIndex

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 101)
    {
       UITextField *field = [alertView textFieldAtIndex:0];
       setTime = [field.text intValue];
       NSLog(@"%d", setTime);

       if( buttonIndex == 0)
       {
          NSLog(@"Ok");
          [self setTimer];
          [self countDownTimerPrepare];
          [self countDownTimerIntro];
          [self prepareForIntroAnimation];
          [self performIntroAnimation];
          [self categoriesList];
       }
       else if (buttonIndex == 1)
       {
          NSLog(@"Cancel");
          ChooseViewController * controller = [[ChooseViewController alloc] initWithNibName:@"ChooseViewController" bundle:nil];
         [self presentViewController:controller animated:YES completion:nil];
       }
   }
  else if (alertView.tag==102)
  {
     if(buttonIndex == 0)
     {
        NSLog(@"Restart");
     }
     else if (buttonIndex == 1)
     {
       NSLog(@"Exit");
     }
  }
}
于 2013-09-11T14:27:28.247 回答
1
   - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
   {
   UITextField *field = [alertView textFieldAtIndex:0];
   setTime = [field.text intValue];

这是错误,您的第二个 alertView 不包含任何文本字段。正如文档中所说:

检索索引处的文本字段 - 当 textFieldIndex 越界时引发 NSRangeException。索引 0 处的字段将是第一个文本字段(单个字段或登录字段),索引 1 处的字段将是密码字段。

这是一个应该有效的解决方案

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if (alertView.tag == 101 && buttonIndex == 0) {

   UITextField *field = [alertView textFieldAtIndex:0];
   setTime = [field.text intValue];

   NSLog(@"%d", setTime);

   ...
}
于 2013-09-11T14:29:19.933 回答