【问题标题】:Mysterious UIButton crash神秘的 UIButton 崩溃
【发布时间】:2012-01-15 14:33:20
【问题描述】:

我有一个 UITableViewCell 有 2 个 UIButton 实例。两个按钮都有一个默认图像。只有“坏”按钮具有禁用状态的图像。仅此而已。

在单元格类中,这两个按钮被声明为带有 IBOutlet 关键字的属性:

@property (nonatomic, readonly) IBOutlet UIButton* buttonCall;
@property (nonatomic, readonly) IBOutlet UIButton* buttonGPS;

.xib 中的 2 个按钮与类中的属性之间建立了联系。

所以,当加载 .xib 时,会完成以下操作:

MyCell* cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
if (nil == cell)
{
    NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil];
    cell = [nibContents objectAtIndex:0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

// gps button
[cell.buttonGPS setTitle:[NSString stringWithFormat:@"%d", indexPath.row] forState:UIControlStateNormal];
[cell.buttonGPS addTarget:self action:@selector(actionGPS:) forControlEvents:UIControlEventTouchUpInside];

if (...condition...)
{
    cell.buttonCall.enabled = NO;
    [cell.buttonCall removeTarget:self action:@selector(actionCall:) forControlEvents:UIControlEventTouchUpInside];
}
else
{
    cell.buttonCall.enabled = YES;
    [cell.buttonCall setTitle:[...TheTitleString...] forState:UIControlStateNormal];
    [cell.buttonCall addTarget:self action:@selector(actionCall:) forControlEvents:UIControlEventTouchUpInside];
}

当视图控制器出现时,它会成功通过(UITableViewCell*)tableView:cellForRowAtIndexPath: 必要的次数。然后应用程序崩溃并出现以下堆栈:

0 kill
...
[UIButton imageRectForContentRect:]
[UIButton(UIButtonInternal) _setupImageView]
[UIButton layoutSubviews]
...
start

我删除了按钮的图像并开始与堆栈崩溃:

0 kill
...
[UIButton layoutSubviews]
...
start

我为按钮添加了一些标识符标签 - 仍然崩溃。

删除了“坏”按钮的 XCode-InterfaceBuilder 连接,崩溃停止了。但是按钮不起作用。

知道导致崩溃的原因吗?

调用栈是:

0   CoreFoundation                      0x013d406e __exceptionPreprocess + 206
1   libobjc.A.dylib                     0x019f0d0a objc_exception_throw + 44
2   CoreFoundation                      0x013d5ced -[NSObject doesNotRecognizeSelector:] + 253
3   CoreFoundation                      0x0133af00 ___forwarding___ + 432
4   CoreFoundation                      0x0133ace2 _CF_forwarding_prep_0 + 50
5   UIKit                               0x0086de3a -[UIButton imageRectForContentRect:] + 350
6   UIKit                               0x0086ed5b -[UIButton(UIButtonInternal) _setupImageView] + 158
7   UIKit                               0x0086e5f8 -[UIButton layoutSubviews] + 693
8   UIKit                               0x0069f322 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 178
9   CoreFoundation                      0x013d5e72 -[NSObject performSelector:withObject:] + 66
10  QuartzCore                          0x0042092d -[CALayer layoutSublayers] + 266
11  QuartzCore                          0x0042a827 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 231
12  QuartzCore                          0x003b0fa7 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 377
13  QuartzCore                          0x003b2ea6 _ZN2CA11Transaction6commitEv + 374
14  QuartzCore                          0x003b2580 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 80
15  CoreFoundation                      0x013a89ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
16  CoreFoundation                      0x0133f670 __CFRunLoopDoObservers + 384
17  CoreFoundation                      0x0130b4f6 __CFRunLoopRun + 1174
18  CoreFoundation                      0x0130adb4 CFRunLoopRunSpecific + 212
19  CoreFoundation                      0x0130accb CFRunLoopRunInMode + 123
20  GraphicsServices                    0x03258879 GSEventRunModal + 207
21  GraphicsServices                    0x0325893e GSEventRun + 114
22  UIKit                               0x00660a9b UIApplicationMain + 1175
23  MyApp                               0x000023f9 main + 169
24  MyApp                               0x00002345 start + 53

例外是:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull length]: unrecognized selector sent to instance 0x146acd8'

info symbol 0x146acd8
__kCFNull in section LC_SEGMENT.__DATA.__data of /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation

第二种情况类似。但是,例外情况完全相同。这是调用堆栈:

0   CoreFoundation                      0x013d406e __exceptionPreprocess + 206
1   libobjc.A.dylib                     0x019f0d0a objc_exception_throw + 44
2   CoreFoundation                      0x013d5ced -[NSObject doesNotRecognizeSelector:] + 253
3   CoreFoundation                      0x0133af00 ___forwarding___ + 432
4   CoreFoundation                      0x0133ace2 _CF_forwarding_prep_0 + 50
5   UIKit                               0x0086e851 -[UIButton layoutSubviews] + 1294
6   UIKit                               0x0069f322 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 178
7   CoreFoundation                      0x013d5e72 -[NSObject performSelector:withObject:] + 66
8   QuartzCore                          0x0042092d -[CALayer layoutSublayers] + 266
9   QuartzCore                          0x0042a827 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 231
10  QuartzCore                          0x003b0fa7 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 377
11  QuartzCore                          0x003b2ea6 _ZN2CA11Transaction6commitEv + 374
12  QuartzCore                          0x003b2580 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 80
13  CoreFoundation                      0x013a89ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
14  CoreFoundation                      0x0133f670 __CFRunLoopDoObservers + 384
15  CoreFoundation                      0x0130b4f6 __CFRunLoopRun + 1174
16  CoreFoundation                      0x0130adb4 CFRunLoopRunSpecific + 212
17  CoreFoundation                      0x0130accb CFRunLoopRunInMode + 123
18  GraphicsServices                    0x03258879 GSEventRunModal + 207
19  GraphicsServices                    0x0325893e GSEventRun + 114
20  UIKit                               0x00660a9b UIApplicationMain + 1175
21  MyApp                               0x000023f9 main + 169
22  MyApp                               0x00002345 start + 53

【问题讨论】:

  • 我们可以查看崩溃详情吗?
  • 还有其他信息吗?如果您需要任何具体的内容,请询问。
  • 实际抛出的异常是什么?
  • 尝试添加@property (nonatomic, retain) IBOutlet UIButton* buttonCall; 而不是@property (nonatomic, readonly) IBOutlet UIButton* buttonCall;。是否可以看到选择器actionCall:的实现方法?
  • 你可以启用僵尸并再次检查问题。

标签: ios interface-builder crash uibutton


【解决方案1】:

从异常中,您似乎认为您正在将 NSString 传递给按钮的标题,而实际上您正在传递 NSNull 的实例。如果您从 JSON 源或类似源获取表视图数据,这很常见 - 如果源字段不存在,则使用 NSNull。

您得到的例外是长度不是 NSNull 的可识别选择器,这是真的,并且它似乎在按钮布置其子视图时出现,当您分配新标题时它会这样做。所以我会把你的调查重点放在那个领域。检查您实际发送的按钮标题的内容。

【讨论】:

    【解决方案2】:
    MyCell* cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (nil == cell)
    {
        NSArray* nibContents = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil];
    cell = [nibContents objectAtIndex:0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.buttonGPS addTarget:self action:@selector(actionGPS:)       forControlEvents:UIControlEventTouchUpInside];
    [cell.buttonCall addTarget:self action:@selector(actionCall:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    // gps button
    [cell.buttonGPS setTitle:[NSString stringWithFormat:@"%d", indexPath.row] forState:UIControlStateNormal];
    
    if (...condition...)
    {
    cell.buttonCall.enabled = NO;
    }
    else
    {
    cell.buttonCall.enabled = YES;
    [cell.buttonCall setTitle:[...TheTitleString...] forState:UIControlStateNormal];
    }
    

    在您的代码中进行这些更改并检查。我认为某些条件可能是错误的,因此 b4 添加目标它已被删除并且在尝试时它崩溃了。

    【讨论】: