xcode [[NSBundle mainBundle] loadNibnamed:owner:options:] 崩溃

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5479684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:51:22  来源:igfitidea点击:

Crash on [[NSBundle mainBundle] loadNibnamed:owner:options:]

iphonexcodeuitableviewnib

提问by user278859

I cannot figure this one out. I have a created a custom UITableViewCell in IB. As far as I can tell I have all the objects in the cell which are all UILabels wired up properly and everything builds without any errors. When I try to use the custom cell the app stops when I try to load the NIB. I've listed what I have in place for this below.

我无法弄清楚这一点。我在 IB 中创建了一个自定义 UITableViewCell。据我所知,我拥有单元格中的所有对象,这些对象都是正确连接的 UILabels,并且一切构建都没有任何错误。当我尝试使用自定义单元格时,应用程序会在我尝试加载 NIB 时停止。我在下面列出了我为此准备的内容。

Any help or hints or where to look to figure this one out would be appreciated.

任何帮助或提示或在哪里寻找解决这个问题将不胜感激。

Thanks,

谢谢,

John

约翰

Here are my UITableViewCell interface and implementation methods. I am not sure I really needed to alloc init the labels. I tried it both ways...

下面是我的 UITableViewCell 接口和实现方法。我不确定我是否真的需要分配初始化标签。我两种方法都试过了...

//  ArrivalTimesCell.h

#import <UIKit/UIKit.h>

@interface ArrivalTimesCell : UITableViewCell {
    IBOutlet UILabel *cityArrivalTimeLabel;
    IBOutlet UILabel *optimumArrivalTimeLabel;
    IBOutlet UILabel *arrivalAvoidTimesLabel;
    IBOutlet UILabel *arrivalTimeNotSetLabel;

}
@property (nonatomic, retain)IBOutlet UILabel *cityArrivalTimeLabel;
@property (nonatomic, retain)IBOutlet UILabel *optimumArrivalTimeLabel;
@property (nonatomic, retain)IBOutlet UILabel *arrivalAvoidTimesLabel;
@property (nonatomic, retain)IBOutlet UILabel *arrivalTimeNotSetLabel;

@end

//  ArrivalTimesCell.m

#import "ArrivalTimesCell.h"

@implementation ArrivalTimesCell
@synthesize arrivalTimeNotSetLabel, arrivalAvoidTimesLabel, optimumArrivalTimeLabel, cityArrivalTimeLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        arrivalTimeNotSetLabel = [[UILabel alloc] init];
        arrivalAvoidTimesLabel = [[UILabel alloc] init];
        optimumArrivalTimeLabel = [[UILabel alloc] init];
        cityArrivalTimeLabel = [[UILabel alloc] init];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];

}

- (void)dealloc{
    [super dealloc];
    [arrivalTimeNotSetLabel release];
    arrivalTimeNotSetLabel = nil;
    [arrivalAvoidTimesLabel release];
    arrivalAvoidTimesLabel = nil;
    [optimumArrivalTimeLabel release];
    optimumArrivalTimeLabel = nil;
    [cityArrivalTimeLabel release];
    cityArrivalTimeLabel = nil;
}

@end

Here is a portion of cellForRowAtIndexPath for the UITableView

这是 UITableView 的 cellForRowAtIndexPath 的一部分

//  TripEditViewController.m

#import "ArrivalTimesCell.h"

@implementation TripEditViewController

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

    CellIdentifier = @"ArrivalTimesCell";
    ArrivalTimesCell *cell = (ArrivalTimesCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ArrivalTimesCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects) {
            if ([currentObject isKindOfClass:[UITableViewCell class]]) {
                cell = (ArrivalTimesCell *) currentObject;
                break;
             }
         }      
    }


    cell.cityArrivalTimeLabel.text = @"Whatever"
    cell.arrivalAvoidTimesLabel.text = @"Whatever";
    cell.optimumArrivalTimeLabel.text = @"Whatever";
    cell.arrivalTimeNotSetLabel.text = @"Whatever";

    return cell;  
}

Program execution stops on the loadNibNamed line without any comment in the console. If I "step into" I get the following which I am not sure has anything to do with the cause of my crash but instead is the result of the nib not loading properly...

程序执行在 loadNibNamed 行停止,控制台中没有任何注释。如果我“进入”,我会得到以下信息,我不确定这与我崩溃的原因有什么关系,而是笔尖加载不正确的结果......

2011-03-29 12:18:14.137 JetLogger[4565:207] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key cityArrivalTimeLabel.' *Call stack at first throw: ( 0 CoreFoundation 0x0111a5a9 exceptionPreprocess + 185 1 libobjc.A.dylib 0x0126e313 objc_exception_throw + 44 2 CoreFoundation 0x0111a4e1 -[NSException raise] + 17 3 Foundation 0x000ca677 _NSSetUsingKeyValueSetter + 135 4 Foundation 0x000ca5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285 5 UIKit 0x0054e30c -[UIRuntimeOutletConnection connect] + 112 6 CoreFoundation 0x010908cf -[NSArray makeObjectsPerformSelector:] + 239 7 UIKit 0x0054cd23 -[UINib instantiateWithOwner:options:] + 1041 8 UIKit 0x0054eab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168 9 JetLogger 0x0004bf18 -[TripEditViewController tableView:cellForRowAtIndexPath:] + 699 10 UIKit 0x003c7b98 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634 11 UIKit 0x003bd4cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75 12 UIKit 0x003d28cc -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561 13 UIKit 0x003ca90c -[UITableView layoutSubviews] + 242 14 QuartzCore 0x02051a5a -[CALayer layoutSublayers] + 181 15 QuartzCore 0x02053ddc CALayerLayoutIfNeeded + 220 16 QuartzCore 0x01ff90b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310 17 QuartzCore 0x01ffa294 _ZN2CA11Transaction6commitEv + 292 18 QuartzCore 0x01ffa46d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99 19 CoreFoundation 0x010fb89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION+ 27 20 CoreFoundation 0x010906e7 __CFRunLoopDoObservers + 295 21 CoreFoundation 0x010591d7 __CFRunLoopRun + 1575 22 CoreFoundation 0x01058840 CFRunLoopRunSpecific + 208 23 CoreFoundation 0x01058761 CFRunLoopRunInMode + 97 24 GraphicsServices 0x01a081c4 GSEventRunModal + 217 25 GraphicsServices 0x01a08289 GSEventRun + 115 26 UIKit 0x00360c93 UIApplicationMain + 1160 27 JetLogger 0x00001ba8 main + 102 28 JetLogger 0x00001b39 start + 53 ) terminate called after throwing an instance of 'NSException'

2011-03-29 12:18:14.137 JetLogger[4565:207] * 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合键值城市到达时间标签的键值编码.' *第一次抛出时调用堆栈:(0 CoreFoundation 0x0111a5a9exceptionPreprocess + 185 1 libobjc.A.dylib 0x0126e313 objc_exception_throw + 44 2的CoreFoundation 0x0111a4e1 - [NSException提高] + 17 3基础0x000ca677 _NSSetUsingKeyValueSetter + 135 4基金会0x000ca5e5 - [NSObject的(的NSKeyValueCoding)的setValue:forKey:] + 285 5的UIKit 0x0054e30c - [ UIRuntimeOutletConnection 连接] + 112 6 CoreFoundation 0x010908cf -[NSArray makeObjectsPerformSelector:] + 239 7 UIKit 0x0054cd23 -[UINib instantiateWithOwner:options:] + 1041 8 UIKit 0x0054UsBundeAbdleNamesAdd:] + 168 9 JetLogger 0x0004bf18 - [TripEditViewController的tableView:的cellForRowAtIndexPath:] + 699 10的UIKit 0x003c7b98 - [UITableView的(UITableViewInternal)_createPreparedCellForGlobalRow:withIndexPath:] + 634 11的UIKit 0x003bd4cc - [UITableView的(UITableViewInternal)_createPreparedCellForGlobalRow:] + 75 12的UIKit 0x003d28cc - [UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561 13的UIKit 0x003ca90c - [UITableView的layoutSubviews] + 242 14 QuartzCore 0x02051a5a - [CALayer的layoutSublayers] + 181 15 QuartzCore 0x02053ddc CALayerLayoutIfNeeded + 220 16 QuartzCore 0x01ff90b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310 17 QuartzCore 0x01ffa294 _ZN2CA11Transaction6commitEv + 292 18 QuartzCore 0x01ffa46d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99 19的CoreFoundation 0x010fb89b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION_ZN2CA11Transaction6commitEv + 292 18 QuartzCore 0x01ffa46d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99 19 CoreFoundation 0x010fb89b __CFALLING_CALLBTION_OBSERF_UNCALLING_CALLBTION_OBSERF_ZN2CA11Transaction6commitEv + 292 18 QuartzCore 0x01ffa46d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99 19 CoreFoundation 0x010fb89b __CFALLING_CALLBTION_OBSERF_UNCALLING_CALLBTION_OBSERF+ 27 20的CoreFoundation 0x010906e7 __CFRunLoopDoObservers + 295 21的CoreFoundation 0x010591d7 __CFRunLoopRun + 1575 22的CoreFoundation 0x01058840 CFRunLoopRunSpecific + 208个23的CoreFoundation 0x01058761 CFRunLoopRunInMode + 97个24 GraphicsServices 0x01a081c4 GSEventRunModal + 217 25 GraphicsServices 0x01a08289 GSEventRun + 115 26的UIKit 0x00360c93 UIApplicationMain + 1160 27 JetLogger 0x00001ba8主+ 102 28 JetLogger 0x00001b39 start + 53 ) 在抛出“NSException”实例后终止调用

回答by ughoavgfhw

Somewhere in your nib, you connect something to the cityArrivalTimeLabel property on an object that does not have that property. Make sure you 1) changed the class of the cell to ArrivalTimesCell and 2) connected that label to the right object.

在您笔尖的某处,您将某些内容连接到没有该属性的对象上的 cityArrivalTimeLabel 属性。确保您 1) 将单元格的类更改为 ArrivalTimesCell 并且 2) 将该标签连接到正确的对象。

You do not need to alloc/init the labels in your init method. They are simply replaced when the nib loads, so it is just a waste of RAM until then.

您不需要在 init 方法中分配/初始化标签。它们只是在笔尖加载时更换,因此在此之前只是浪费 RAM。