iphone - 由 Interface Builder 创建的 UIButton 崩溃了

标签 iphone uiview interface-builder uibutton nib

我已经缩小了一个丑陋的错误范围,但由于它似乎是 Nib /界面生成器内部的东西,所以我不知道下一步该做什么。

我在 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 中访问。我没有任何 IBOutlet 或任何变量链接到该图像。)

那么,嗯,现在怎么办?

编辑:

有时,被捕获为僵尸的不是保留消息,有时是 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];
}

谢谢!

最佳答案

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

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

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

什么东西。希望它能得到很好且真正的修复。

<小时/>

更新:

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

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

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

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

关于iphone - 由 Interface Builder 创建的 UIButton 崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1575529/

相关文章:

iphone - iPhone SDK中所有可用的音频类型是什么?

objective-c - 仅在纵向模式下伪造横向 View

ios - _UIReplicantView 和 snapshotViewAfterScreenUpdates 如何获取其内容?

ios - Objective-C ,避免显示负浮点值

ios - 使用 Swift 从 xib/nib 实例化 UIView

objective-c - 如何将 NIB 文件中的对象发送到前/后?

ios - 为什么 UIBarButtonItems 最小尺寸时宽度为 0

ios - InterfaceBuilder 目标/操作中的 UIGestureRecognizer

iphone - iOS6下如何查看用户是否安装了google map ?

objective-c - 如何在我的应用程序中的 iOS 键盘上创建自定义按钮?