【问题标题】:I Need an Action to Change my NSArray by pressing a button in UIAlertView我需要通过按下 UIAlertView 中的按钮来更改我的 NSArray
【发布时间】:2013-02-22 04:05:25
【问题描述】:

我以编程方式设置了一个 UITableView,它填充了所有状态的名称。当您单击其中一个单元格时,例如。 ALABAMA,将出现一个 UIAlertView 标题是您刚刚单击的状态(ALABAMA)的标题以及与该状态相关的数字,该状态位于另一个 NSMutable 数组中。每个州都有不同的数字与之相关。

这个数字实际上是UIAlert中的占位符文本,警报的目的是让您可以在文本字段中输入一个数字,当您点击更改按钮时,占位符文本中的数字( from the Array) 将随着用户估算的数字而改变;永久。一切都很好,直到我点击更改,然后我收到一个 SIGABRT 错误,并在输出中显示:

2013-02-21 20:57:33.750 final[10046:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: object cannot be nil'

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


UIAlertView *alertView = [[UIAlertView alloc]
                        initWithTitle:[NSString stringWithFormat:@"%@", [states objectAtIndex:indexPath.row]]
                        message:[NSString stringWithFormat:@"%@", [tax objectAtIndex:53]]
                        delegate:self       
                        cancelButtonTitle:@"Cancel"           
                        otherButtonTitles:@"Change", nil];

    UITextField *myTextField =[[UITextField alloc] initWithFrame:CGRectMake(42, 50, 200, 25)];
    CGAffineTransform myTransform =CGAffineTransformMakeTranslation(0, 0);
    [alertView setTransform:myTransform];
    [myTextField setBackgroundColor:[UIColor clearColor]];
    [alertView addSubview:myTextField];
    myTextField.placeholder = [NSString stringWithFormat:@"%@", [tax objectAtIndex:indexPath.row]];
    myTextField.borderStyle = UITextBorderStyleNone;
    myTextField.returnKeyType = UIReturnKeyDone;
    myTextField.textColor = [UIColor grayColor];
    myTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    myTextField.textAlignment = UITextAlignmentCenter;
    myTextField.delegate = self;


    [alertView show];
    [alertView release];
}

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

    if (buttonIndex == 1) {

        NSString *theTaxer = [myTextField text];
        [tax replaceObjectAtIndex:1 withObject:theTaxer];
    }
}

【问题讨论】:

    标签: objective-c indexing nsmutablearray uialertview ibaction


    【解决方案1】:

    在调用 alertView:clickedBUttonAtIndex 时,不再定义 myTextField,因为 myTextField 未定义为类 ivar,您只能在创建它的函数中引用它。如果要引用 @ 987654323@ 类中的其他位置(例如在 alertView 回调中)使其成为类 iVar。

    【讨论】:

      【解决方案2】:

      试试这个,

      - (void)tableView:(UITableView *)tableView 
                didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      
      
      UIAlertView *alertView = [[UIAlertView alloc]
                          initWithTitle:[NSString stringWithFormat:@"%@", [states objectAtIndex:indexPath.row]]
                          message:[NSString stringWithFormat:@"%@", [tax objectAtIndex:53]]
                          delegate:self       
                          cancelButtonTitle:@"Cancel"           
                          otherButtonTitles:@"Change", nil];
      alertView.tag =indexPath.row;
      
      UITextField *myTextField =[[UITextField alloc] initWithFrame:CGRectMake(42, 50, 200, 25)];
      [myTextField setTag:99];
      CGAffineTransform myTransform =CGAffineTransformMakeTranslation(0, 0);
      [alertView setTransform:myTransform];
      [myTextField setBackgroundColor:[UIColor clearColor]];
      [alertView addSubview:myTextField];
      myTextField.placeholder = [NSString stringWithFormat:@"%@", [tax objectAtIndex:indexPath.row]];
      myTextField.borderStyle = UITextBorderStyleNone;
      myTextField.returnKeyType = UIReturnKeyDone;
      myTextField.textColor = [UIColor grayColor];
      myTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
      myTextField.textAlignment = UITextAlignmentCenter;
      myTextField.delegate = self;
      
      
      [alertView show];
      [alertView release];
      }
      
      -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
      
      if (buttonIndex == 1) {
        UITextField * myTextField = (UITextField *)[alertView viewWithTag:99];
          NSString *theTaxer = [myTextField text];
          [tax replaceObjectAtIndex:1 withObject:theTaxer];
      }
      }
      

      【讨论】:

        【解决方案3】:

        Shizam 是对的,我认为如果你想获得 textview 的引用,只需在创建 alertview 时给 textview 一个标签:myTextField.tag = 111; 然后在 alertView 委托方法中:

        -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        
        if (buttonIndex == 1) {
        
            UITextField *textField = [alertView viewWithTag:111];
            NSString *theTaxer = [textField text];
            [tax replaceObjectAtIndex:1 withObject:theTaxer];
        }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-15
          • 1970-01-01
          相关资源
          最近更新 更多