iphone - 可以重置 UIButton 背景图片吗?

标签 iphone ios cocoa-touch uibutton

我有一个应用程序,我最初设置了 UIButton 的背景图像,然后需要将其更改为其他内容。我确实意识到我可以删除原始按钮并使用新图像分配一个新按钮,但我更愿意提高效率并重用我已经分配的对象。有可能这样做吗?我注意到 currentBackgroundImage 属性是只读的,所以当我尝试像这样的东西时:

        [thumbnailButton setBackgroundImage:nil forState:UIControlStateNormal];
        [thumbnailButton setBackgroundImage:[UIImage imageWithCGImage:[[photos objectAtIndex: currentPhotoIndex] thumbnail]] forState:UIControlStateNormal];

或者只是:

        [thumbnailButton setBackgroundImage:[UIImage imageWithCGImage:[[photos objectAtIndex: currentPhotoIndex] thumbnail]] forState:UIControlStateNormal];

我得到以下信息:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView setBackgroundImage:forState:]: unrecognized selector sent to instance 0x16c570'

是否可以使用 UIButton 来完成,还是我只需要删除原始按钮并创建一个新按钮?

最佳答案

你这里有内存管理问题。

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView setBackgroundImage:forState:]: unrecognized selector sent to instance 0x16c570'

这意味着您的 thumbnailButton 没有指向 UIButton,而是指向 UITableViewCellContentView。这可能是由于分配不当或 UIButton 被释放(在这种情况下您有一个悬空指针)。运行分析器并仔细检查您对按钮的使用。

UIButton buttonWithType 返回一个自动释放的实例,所以你需要保留它。

关于iphone - 可以重置 UIButton 背景图片吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9218484/

相关文章:

iphone - 无法设置深度缓冲区?

ios - 适用于 iOS 5 的地理围栏 API

ios - UIBarButtonItem:如何更改文本阴影偏移量?

ios - 后台任务有限制吗?

iphone - 如何使用使用 Interface Builder 创建的 nib 文件加载 UIView

iphone - 使用开始时间和持续时间计算结束时间-iOS

objective-c - - (id)copyWithZone :(NSZone *)zone - memory leaks

swift - 用于在管道结果时链接异步操作的 GCD 模式

iphone - 推送和弹出 View Controller - 动画

ios - Apple Siri Intent 无明显原因停止播放