【问题标题】:(iOS) crash when view controller property is set(iOS) 设置视图控制器属性时崩溃
【发布时间】:2018-05-18 01:35:50
【问题描述】:

我在将数据传递给新的视图控制器时遇到问题。

我有两个 VC。在“FirstVC”中有一个带有动态原型单元格的表格视图。通用单元格包含一个标签和一个按钮,并分配给它自己的类“GenericCell”。我想在按下按钮时将单元格标签的文本传递给 SecondVC,但应用程序崩溃并出现错误:

Interface Builder 文件中的未知类 _TtC13testTableView37SecondVC。

UIViewController setMyString:]:无法识别的选择器发送到实例 0x7fa05e418110

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UIViewController setMyString:]:无法识别的选择器发送到实例 0x7fc8add05710

我试过了:

  1. 在 getText 方法之外使用 segue(使用 prepareForSegue 方法)

    1a。从 getText 方法外部推送 VC

  2. 实现和调用myString的set/get方法

  3. 向 myString 属性添加属性

但应用程序同样崩溃。

GenericCell.h

#import <UIKit/UIKit.h>
@protocol MyCellDelegate
-(void)getText:(NSString *)text;
@end

@interface GenericCell: UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel * label;
@property (assign, nonatomic) id <MyCellDelegate> delegate;
@end

GenericCell.m

#import "GenericCell.h"
@interface GenericCell()
@end

@implementation GenericCell

- (IBAction)buttonPressed {
    if (self.delegate) {
        [self.delegate getText:self.label.text];
    }
}
@end

FirstVC.m

#import "FirstVC.h"
#import "GenericCell.h"
#import "SecondVC.h"

@interface FirstVC() <UITableViewDataSource,UITableViewDelegate, MyCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property NSString * idToPass;
@end

@implementation FirstVC


NSMutableArray<NSString *> *array;

- (void)viewDidLoad {

    array = [[NSMutableArray alloc]initWithObjects:@"0x22", @"0x11", @"0x24", nil];

    [super viewDidLoad];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    self.tableView.rowHeight=100;
}

-(void)getText:(NSString *)text {
   self.idToPass = text;

   SecondVC * secondVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondVC"];
   secondVC.myString = self.idToPass;
   [[self navigationController] pushViewController:secondVC animated:YES];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return array.count;
}

- (GenericCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

    GenericCell * cell = [[self tableView]dequeueReusableCellWithIdentifier:@"GenericCell" forIndexPath:indexPath];

    cell.delegate = self;
    cell.label.text = [array objectAtIndex:indexPath.row];
    return cell;
}
@end

SecondVC.h

#import <UIKit/UIKit.h>
@interface SecondVC : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *label;
@property NSString * myString;
@end

SecondVC.m

#import "SecondVC.h"

@interface SecondVC ()
@end

@implementation SecondVC

- (void)viewDidLoad {
    self.label.text = self.myString;
    [super viewDidLoad];
}

@end

“idToPass”已通过委托正确设置。

【问题讨论】:

  • "'NSInvalidArgumentException': unrecognized selector sent to instance" 显示整个错误消息,它应该提供有关调用的方法(可能是 getter 或 setter)的信息,以及“不匹配的类” "。
  • 检查情节提要中的 SecondVC 是否真的是在身份检查器的 Class 字段中设置的 SecondVC 对象。错误消息说这是一个普通的UIViewController
  • @PhillipMills 故事板 ID?
  • 没有任何迹象表明您的 ID 有误。我比较怀疑你没有设置自定义类名。
  • 现在我查看了 both 错误,这似乎很奇怪:“Interface Builder 文件中的未知类 _TtC13testTableView37SecondVC。”您是否将其设置为模块的一部分?

标签: ios objective-c xcode uitableview uiviewcontroller


【解决方案1】:

这对我有用:在 buttonPressed 方法中像这样更新 if 语句

if ([self.delegate respondsToSelector:@selector(getText:)]){
        [self.delegate getText:self.label.text];
}

向 SecondVC 提供数据正在使用的另一个选项

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    SecondVC * vc = segue.destinationViewController;
    if ([segue.identifier isEqualToString: @"yourSegue"]) {
        vc.myString = self.idToPass;
    }
}

我建议的一个提示是检查(使用简单的 if 语句)您在 SecondVC 中的对象是 nil 还是 !nil

希望对你有所帮助:)

【讨论】:

    【解决方案2】:

    在 getText 方法中,您将文本分配给 myProperty 而不是 myString...

    应该是:

    secondVC.myString = self.idToPass;
    

    【讨论】:

    • 如果我在这里抄得不好,我很抱歉。我马上改正
    猜你喜欢
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多