iOS:调用存储函数时文本字段输入崩溃

iOS : crash on textfield input while calling store function

尽管我确实得到了一些帮助,但我无法仅从提示中轻松找到修复程序,因为我对此很陌生,这是一个已有几年历史的应用程序,现在我正努力让它发挥作用' 标准,虽然这不是我的主要领域,但目前是我的任务。

我有一个保存输入数据的表单,但它在第一次输入时崩溃,无论我选择从哪个文本字段开始,我都从这一行得到错误:

 [self saveValue:cell.textInput.text forRow:path.row atSection:path.section];

错误:

[UITableViewWrapperView textInput]: unrecognized selector sent to instance 0x106958c00

上下文:

-(void)textFieldDidChange:(NSNotification *)notif {

atelierFormInputCell *cell = (atelierFormInputCell*) [[[(UITextField*)notif.object superview] superview] superview] ;
NSIndexPath *path = [form indexPathForCell:cell];
[self saveValue:cell.textInput.text forRow:path.row atSection:path.section];
}

保存值函数:

-(BOOL)saveValue:(NSString *)value forRow:(NSInteger) row atSection:(NSInteger) section {

switch (section) {
    case 0:
        switch (row) {
            case 0:
                car.marque = value;
                break;
            case 1:
                car.modele = value;
                break;
            case 2:
                car.mise_en_circul = value;
                break;
            case 3:
                car.immatriculation = value;
                break;
            case 4:
                car.num_serie = value;
                break;
        }
        break;
    case 1:
        switch (row) {
            case 0:
                car.code_clef = value;
                break;
            case 1:
                car.code_autoradio = value;
                break;
            case 2:
                car.taille_pneu_avant = value;
                break;
            case 3:
                car.taille_pneu_arriere = value;
                break;
            case 4:
                car.pression_pneu_avant = value;
                break;
            case 5:
                car.pression_pneu_arriere = value;
                break;
        }
        break;
    case 2:
        switch (row) {
            case 0:
                car.type_huile = value;
                break;
        }
        break;
    case 3:
        switch (row) {
            case 0:
                car.date_achat = value;
                break;
            case 1:
                car.km_init = value;
                break;
        }
        break;
    case 4:
        switch (row) {
            case 0:
                car.nom_assurance = value;
                break;
            case 1:
                car.num_assurance = value;
                break;
        }
        break;
    case 5:
        switch (row) {
            case 0:
                if ([car.choix_last_entretien isEqualToString:@"date"]) car.date_last_entretien = value;
                else car.km_last_entretien = value;
                break;
            case 1:
                if ([car.choix_next_entretien isEqualToString:@"date"]) car.date_next_entretien = value;
                else car.km_next_entretien = value;

                break;
            case 2:
                car.date_last_ct = value;
                break;
            case 3:
                car.date_next_ct = value;
                break;
        }
        break;
    case 6:
        switch (row) {
            case 0:
                car.notes = value;
                break;
        }
        break;
}
return YES;

}

根据我尝试更改的建议:

 atelierFormInputCell *cell = (atelierFormInputCell*) [[[(UITextField*)notif.object superview] superview] superview] ;

atelierFormInputCell *cell =(atelierFormInputCell *) [(UITextField*)notif superview];

但它在同一点崩溃,给我这个错误: [NSConcreteNotification 超级视图]:无法识别的选择器发送到实例 0x1702580f0

行:

 atelierFormInputCell *cell =(atelierFormInputCell *) [(UITextField*)notif superview];

所以我不知道这是进步还是更糟。

使用 atelierFormInputCell 文件更新

atelierFormInputCell.h :

 @interface atelierFormInputCell : UITableViewCell {
UILabel *label;
UITextField *textInput;
NSUInteger row;
}

atelierFormInputCell.m :

 @implementation atelierFormInputCell
 @synthesize label, textInput, row;

 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    UIColor *gris = [UIColor colorWithRed:108/255.0 green:116/255.0 blue:120/255.0 alpha:1.0];
    UIColor *black = [UIColor blackColor];
    UIColor *transparent = [UIColor clearColor];

    label = [[UILabel alloc]init];
    label.textAlignment = UITextAlignmentLeft;
    label.font = [UIFont fontWithName:@"FontType-Normal" size:14.0];
    label.backgroundColor = transparent;
    label.textColor = gris;
    label.numberOfLines = 1;



    textInput = [[UITextField alloc] init];
    textInput.borderStyle = UITextBorderStyleNone;
    textInput.textAlignment = UITextAlignmentRight;
    textInput.backgroundColor = transparent;
    textInput.font = [UIFont fontWithName:@"FontType-Normal" size:14.0];
    textInput.backgroundColor = transparent;
    textInput.textColor = black;
    textInput.returnKeyType = UIReturnKeyDone;

    [self.contentView addSubview:label];
    [self.contentView addSubview:textInput];

    self.selectionStyle = UITableViewCellSelectionStyleNone;
     }
     return self;
     }

上面的原因是一个完美的例子,为什么我们不应该在我们的应用程序中硬编码视图层次结构。视图层次结构可以从 os 版本更改为 os 版本,因此应该完全忘记整个方法。我知道这不是您的代码,但作为将来的提示,请尽量避免将来出现这种情况。例如,使用委托模式会很棒。

我给你写了一个方法,应该找什么,如果视图层次中有atelierFormInputCell

- (atelierFormInputCell*)findCell:(UIView*)fromView {
    UIView *view = fromView;
    do {
        if ([view isKindOfClass:[atelierFormInputCell class]]) {
            NSLog(@"found");
            return (atelierFormInputCell*)view;
        } else {
            view = view.superview;
        }
    } while(view != nil);
    NSLog(@"not found");
    return nil;
}

这是一种动态方法,查找给定对象的超视图,并检查它们是否是搜索到的类型。如果是,它 return 是单元格,如果不是,它将 return nil.

所以,而不是这一行

atelierFormInputCell *cell = (atelierFormInputCell*) [[[(UITextField*)notif.object superview] superview] superview] ;

在你的viewController中添加函数,并按如下方式调用

 atelierFormInputCell *cell = [self findCell:(UIView*)notif.object];

如果它能解决您的问题,请告诉我,如果不能,我会编辑我的答案。