-1

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

这个数字实际上是 UIAlert 中的占位符文本,警报的目的是让您可以在文本字段中输入一个数字,当您点击更改按钮时,占位符文本中的数字(来自 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];
    }
}
4

3 回答 3

0

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];
}
}
于 2013-02-22T04:32:25.583 回答
0

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

于 2013-02-22T04:19:07.820 回答
0

尝试这个,

- (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];
}
}
于 2013-02-22T05:03:41.830 回答