【问题标题】:iOS : crash on textfield input while calling store functioniOS:调用存储功能时文本字段输入崩溃
【发布时间】:2016-12-19 14:11:13
【问题描述】:

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

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

 [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 superview]:无法识别的选择器发送到实例 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;
     }

【问题讨论】:

  • 能否请您显示atelierFormInputCell界面?
  • @dirtydanee 已编辑答案:)

标签: ios xcode save unrecognized-selector


【解决方案1】:

上面的理由就是一个完美的例子,为什么我们不应该在我们的应用程序中对视图层次结构进行硬编码。视图层次结构可以从 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;
}

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

所以,而不是这一行

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

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

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

如果它解决了你的问题,请告诉我,如果没有,我会编辑我的答案。

【讨论】:

  • 它确实有效!但是,有一个小错误(可能与它无关):如果我滚动查看第一个文本字段,如果第一个文本字段不可见(因为它已被滚动),我的第一个文本字段值将更新为我输入的最新文本字段值在文本字段下方输入文本字段时,不会发生这种情况
  • 嗯,这似乎是一个不同的问题。如果此答案解决了您最初的问题,请关闭此问题。在一个新问题中,发布您的下一个问题。肯定会有人捡起来的!
猜你喜欢
  • 2013-02-10
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多