【问题标题】:UIButton, created by Interface Builder, is crashing由 Interface Builder 创建的 UIButton 正在崩溃
【发布时间】:2010-12-07 05:26:46
【问题描述】:

我已经缩小了一个丑陋的错误,但由于它似乎是 nib/Interface Builder 内部的东西,我不知道下一步该做什么。

我在 IB 中创建了一个 UIView,用作自定义对话框。它显示一条消息和两个按钮。 (继续或取消。)两个按钮都在 Interface Builder 中设置了背景图像。

处理取消按钮的背景图像的方式有问题。使用 NSZombieEnabled,我一直在运行该程序。大多数情况下,以下方法会记录以下内容:

-[ModalDialog showInView:title:message:cancelText:proceedText:]
dialogProceedButton <UIButton: 0x7031f10; frame = (286 192; 90 31) // (etc.)
dialogProceedButtonBackground <UIImage: 0x3b36120>
dialogCancelButton <UIButton: 0x3b39cd0; frame = (104 192; 90 31) // (etc.)
dialogCancelButtonBackground <UIImage: 0x3b3a920>

这完全正常。然而,有时它会这样做(如果我通过快速点击一些界面按钮来“冲”UI,我可以让它在某种程度上可靠地重复):

-[ModalDialog showInView:title:message:cancelText:proceedText:]
dialogProceedButton <UIButton: 0x7031f10; frame = (286 192; 90 31) // (etc.)
dialogProceedButtonBackground <UIImage: 0x3b36120>
dialogCancelButton <UIButton: 0x3b39cd0; frame = (104 192; 90 31) // (etc.)
*** -[UIImage retain]: message sent to deallocated instance 0x3b3a920

如您所见,NSZombieEnabled 发现取消按钮的背景图像已被释放,但正在发送保留消息。 (虽然不是我...该图像用于这个按钮,并且在Interface Builder中访问。我没有任何IBOutlets或任何变量链接到该图像。)

那么,嗯,现在呢?

编辑:

有时,被捕获为僵尸的不是保留消息,有时是 isKindOfClass:

//(the object address is always dialogCancelButton.currentBackgroundImage)
-[UIImage isKindOfClass:]: message sent to deallocated instance 0x1661f0
//occasionally, these come along, too:
*** NSInvocation: warning: object 0x7e0d0b0 of class '_NSZombie_UIImage' does not implement methodSignatureForSelector: -- trouble ahead
*** NSInvocation: warning: object 0x7e0d0b0 of class '_NSZombie_UIImage' does not implement doesNotRecognizeSelector: -- abort

这是我自定义的 UIViews "showInView" 方法:

- (void)showInView:superView 
             title:(NSString*)title 
             message:(NSString*)message 
             cancelText:(NSString*)cancelText 
             proceedText:(NSString*)proceedText {

NSLog(@"%s",__PRETTY_FUNCTION__);
NSLog(@"dialogProceedButton %@", dialogProceedButton);
NSLog(@"dialogProceedButtonBackground %@", dialogProceedButton.currentBackgroundImage);
NSLog(@"dialogCancelButton %@", dialogCancelButton);
NSLog(@"dialogCancelButtonBackground %@", dialogCancelButton.currentBackgroundImage);



CGRect rect;
dialogTitle.text = title;
dialogMessage.text = message;
[dialogProceedButton setTitle:proceedText forState:UIControlStateNormal];

if (cancelText == @"") { // SINGLE BUTTON DIALOG
  dialogCancelButton.hidden = YES;
  rect = [dialogProceedButton frame];
  rect.origin.x = 195; //center the button 
  [dialogProceedButton setFrame:rect];
} else {
  [dialogCancelButton setTitle:cancelText forState:UIControlStateNormal];
  dialogCancelButton.hidden = NO;
  rect = [dialogProceedButton frame];
  rect.origin.x = 286; //button on right of dialog box 
  [dialogProceedButton setFrame:rect];
}

[UIView beginAnimations:@"modalAppears" context:nil];
[UIView setAnimationDuration:0.5];
[superView addSubview:self];
self.alpha = 1.0;
[UIView commitAnimations];
}

谢谢!

【问题讨论】:

    标签: iphone uiview interface-builder uibutton nib


    【解决方案1】:

    好的,这就是个什么鬼东西。我决定尝试反转“继续”和“取消”按钮的图像。现在的结果是 Proceed 按钮图像会导致崩溃(就像间歇性一样)。我从我的项目和 Interface Builder 中完全删除了图像。然后我添加了一个新的副本并使用了新名称并将其连接起来。

    使用之前的设置,我能够在大约 40% 的时间内重现崩溃。在这些更改之后,我已经尝试了大约 20 次来重现崩溃,但我现在根本无法重现它。

    如果图像或笔尖损坏,为什么,为什么,为什么会导致随机/间歇性症状?

    什么。希望它得到很好的修复。


    更新:

    而且...所以还有更多内容。结果我发现我在(不完整的)说明视图中巧合地使用了相同的图像作为占位符。为了暂时方便,我使用 [UIImage imageNamed:] 来抓取图像。它被正确分配和释放,但似乎 IB 与 imageNamed: 方法和/或缓存的配合并不完美。

    当我去抓取图像的新副本时,我还给它一个新的名称,这意味着现在 IB 按钮图像和临时占位符图像不再相同完全没有图像。

    我回到几天前的项目备份来测试我的理论。我所做的只是告诉指令视图使用不同的占位符图像。崩溃停止。

    那么,这可能是一个 SDK 错误。应该没有任何理由不在 IB 中使用图像,并且在其他地方使用 imageNamed: 使用相同的图像。如果我在这些日子里感到狡猾或无聊,也许我会将其提炼成一个示例项目以发送到 Apple 雷达。

    【讨论】:

    • 这对我来说似乎超级,超级不可能 - 我认为很多人一直使用图像名称和 IB。不过,测试一下会很有趣。
    【解决方案2】:

    您的 XIB 文件如何连接到您的视图?您定义了哪些 IBOutlets?我真的怀疑你是否按照你描述的方式解决了你的问题。

    【讨论】:

    • 我同意这似乎不应该是问题的原因(或解决方法),但自从我做了那件事以来,它没有一次崩溃。在我确信足以继续前进之前,我可能最终尝试了大约 50 次来重现崩溃。在更改之前,我每尝试 2 到 4 次就可以让崩溃发生一次。 耸耸肩
    • (果然,还有更多内容......请参阅我的完整答案中的更新。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2022-11-28
    相关资源
    最近更新 更多