【问题标题】:"unrecognized selector sent to instance" When setting a UILabel's text“无法识别的选择器发送到实例”设置 UILabel 的文本时
【发布时间】:2014-04-02 02:39:10
【问题描述】:

我是一名 iOS 编程学生,正在阅读 Big Nerd Ranch 书籍。我看到很多其他人已经捕获了相同的异常“无法识别的选择器发送到实例”,但在我的代码中无法找出导致它的原因。

它发生在我尝试设置 UITableViewCell 的 UILabel 的文本的这种方法中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    BNRItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BNRItemCell" forIndexPath:indexPath];
    NSArray *items = [[BNRItemStore sharedStore] allItems];
    //Set BNRItem instances to equal a given object in the items[] array
    BNRItem *item = items[indexPath.row];
    cell.nameLabel.text = item.itemName;  //This is the line where the exception happens
    cell.serialNumberLabel.text = item.serialNumber;
    cell.valueLabel.text = [NSString stringWithFormat:@"$%d", item.valueInDollars];
    cell.thumbnailView.image = item.thumbNail;

    return cell;

}

在 BNRItem 实现中,我使用归档来存储其属性的值(用于设置文本标签中的文本,这是引发异常的地方)。

-(void) encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.itemName forKey:@"itemName"];
    [aCoder encodeObject:self.serialNumber forKey:@"serialNumber"];
    [aCoder encodeObject:self.dateCreated forKey:@"dateCreated"];
    [aCoder encodeObject:self.itemKey forKey:@"itemKey"];
    [aCoder encodeInt:self.valueInDollars forKey:@"valueInDollars"];
    [aCoder encodeObject:self.thumbNail forKey:@"thumbnail"];
}



- (instancetype) initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self)
    {
        _itemName = [aDecoder decodeObjectForKey:@"itemName"];
        _serialNumber= [aDecoder decodeObjectForKey:@"serialNumber"];
        _dateCreated = [aDecoder decodeObjectForKey:@"dateCreated"];
        _itemKey = [aDecoder decodeObjectForKey:@"itemKey"];
        _valueInDollars = [aDecoder decodeIntForKey:@"valueInDollars"];
        _thumbNail = [aDecoder decodeObjectForKey:@"thumbnail"];


    }
    return self;
}



- (id) initWithItemName:(NSString *)name
         valueInDollars: (int) value
           serialNumber: (NSString *) sNumber;
    {

    self = [super init];


    //Give the instance variables initial values
    [self setItemName:name]; //point to self since its the initializer of the object
    [self setSerialNumber:sNumber];
    [self setValueInDollars:value];
    _dateCreated = [[NSDate alloc]init];

    NSUUID *uuid = [[NSUUID alloc] init];
    NSString *key = [uuid UUIDString];
    _itemKey = key;

    return self; //return self so that you can assign it to a variable
}

编辑 - 这是 BNRItem 的其余实现。即,一种随机化 BNRItem 的属性的方法和另一种给它一个缩略图的方法

//Creates a BNRItem instance with a random name, random value and random serial number
+ (id)randomItem {
//Create an array of three adjectives
NSArray *randomAdjectiveList = [NSArray arrayWithObjects:@"John", @"Kyle", @"Jerry", nil];

//Create an array of three nouns
NSArray *randomNounList = [NSArray arrayWithObjects:@"Cheese", @"Meat", @"Vegetables",    
nil];

NSInteger adjectiveIndex = rand() % [randomAdjectiveList count];
NSInteger nounIndex = rand() % [randomNounList count];

NSString *randomName = [NSString stringWithFormat:@"%@ %@", [randomAdjectiveList   
objectAtIndex:adjectiveIndex], [randomNounList objectAtIndex:nounIndex]];

int randomValue = rand () % 100;

NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                '0' + rand() % 10,
                                'A' + rand() % 26,
                                '0' + rand() % 10,
                                'A' + rand() % 26,
                                '0' + rand() % 10 ];

BNRItem *newItem = [[self alloc] initWithItemName:randomName
                                   valueInDollars:randomValue
                                     serialNumber:randomSerialNumber];

return newItem;

}




- (void) setThumbNailFromImage:(UIImage *)image
{
CGSize origImageSize = image.size;

CGRect newRect = CGRectMake(0, 0, 40, 40);

//Figure out a scaling ratio to make sure we maintain the same aspect ratio
float ratio = MAX(newRect.size.width / origImageSize.width, newRect.size.height /  
origImageSize.height);

//Createa a transparent bitmap context with a scaling factor equal to that of the screen

UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);

//Create a path that is a rounded rectangle
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:5.0];

//Make all subsequent drawing clip to this rounded rectangle
[path addClip];

//Center the image in the thumbnail rectangle
CGRect projectRect;
projectRect.size.width = ratio * origImageSize.width;
projectRect.size.height  = ratio * origImageSize.height;
projectRect.origin.x = (newRect.size.width - projectRect.size.width) / 2.0;
projectRect.origin.y = (newRect.size.height - projectRect.size.height);


//Draw the image on it
[image drawInRect:projectRect];

//Get the image from the image context; keep it as our thumbnail
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
self.thumbNail = smallImage;

//Clea image context resources; we're done
UIGraphicsEndImageContext();


}

UITableViewCell 是用 xib 创建的,有 4 个出口(3 个标签和一个图像)。

#import <Foundation/Foundation.h>

@interface BNRItemCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *serialNumberLabel;
@property (weak, nonatomic) IBOutlet UILabel *valueLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *thumbnailView;

@end

我还没有学习 Core Data,但它即将到来,问题是我需要得到我正在工作的东西,以便我可以根据教科书的说明继续修改它。任何帮助表示赞赏!

【问题讨论】:

  • BNRItemCell 是否有序列号标签,UILabel 类型的属性?
  • 是的。我正在编辑 OP 并为 BNRItemCell 添加 .h
  • -1 没有引用准确和完整的错误信息。
  • 正如@HotLicks 指出的:请向我们展示完整的错误消息。问题可能出在您的BNRItem 上吗?
  • 你知道你在哪一行得到这个错误吗??

标签: ios objective-c xcode exception


【解决方案1】:

serialNumber: (NSString *) sNumber; 之后有一个额外的;,这会导致错误。删除它,然后重试。

【讨论】:

  • 感谢您的敏锐眼光,我摆脱了它,但仍然出现错误。
  • 您是否在 Storyboard 中链接了 UITableViewIBOutlet
  • 不,不在情节提要中。我只是从 xib 中拖动控件。
  • 我假设你已经导入了UIKit,对吧?我看到你包含了Foundation,但没有包含UIKit(好吧,但你可以运行该应用程序,所以你可能做到了)。尝试逐行注释掉代码以定位错误
  • 是的,当我注释掉将 UITableViewCell 的标签指向 BNRItem 属性的行时,错误消失了。这肯定与 BNRItem 类有关,我发布了该类的其余部分的实现。
【解决方案2】:

试试这个可能对你有帮助

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

 BNRItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BNRItemCell"  forIndexPath:indexPath];
if (cell == nil)
{
 cell = [[BNRItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

NSArray *items = [[BNRItemStore sharedStore] allItems];
//Set BNRItem instances to equal a given object in the items[] array
BNRItem *item = [items objectAtIndex:indexPath.row];// try this as well
cell.nameLabel.text = [NSString stringWithFormat:@"%@",item.itemName];  // must try this
cell.serialNumberLabel.text = item.serialNumber;
cell.valueLabel.text = [NSString stringWithFormat:@"$%d", item.valueInDollars];
cell.thumbnailView.image = item.thumbNail;

return cell;

}

【讨论】:

  • 啊仍然收到错误:(。具体消息是“2014-04-02 15:30:43.352 Homepwner[34104:90b] -[__NSCFString itemName]: unrecognized selector sent to instance 0x10951fe00”
  • 啊,还是不行。代码也直接从 BNR 教科书复制而来,我假设代码本身没有功能障碍。导致这种情况的文件存储方式是否与此有关?
  • 感谢您的帮助
  • 问题是您的 itemname 没有任何价值,您正在将其文本设置为标签,这就是它给您该消息的原因
【解决方案3】:

我也遇到了这个问题。我进入 BNRItemCell.xib 文件并打开文档大纲,右键单击视图树中的一些标签以检查参考出口。我注意到它们与 xib 中的文件所有者以及 BNRItemCell.h 文件中的 IBOutlet 相关联。我从文件所有者那里解开了它们,它解决了这个问题。不确定这是否是您的问题,但请检查一下。

【讨论】:

  • 不幸的是我放弃了它,我有一个课程的最终作业应用程序,所以我跳进去了,把 BNR 教程抛在了后面。不过感谢您的回复,如果我以后遇到这个问题,那绝对不值得。
猜你喜欢
  • 1970-01-01
  • 2015-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 2012-07-24
相关资源
最近更新 更多