我有一个应用程序,我最初设置了 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/