UIButton 设置代码生成的警告
我有一个 for 循环在按钮上设置背景图像,基本上按钮是不同项目和项目的缩略图预览。无法静态设置,但是代码会发出警告,因为它会遍历所有 UIView,但随后调用 setBackgroundImage,但该方法并不适用于所有视图。该警告令人恼火,我明白它在抱怨什么,我怎样才能摆脱它? (我不想关闭警告,我想解决问题)
// For loop to set button images
for (UIView *subview in [self.view subviews]) // Loop through all subviews
{
// Look for the tagged buttons, only the 8 tagged buttons & within array bounds
if((subview.tag >= 1) && (subview.tag <= 8) && (subview.tag < totalBundles))
{
// Retrieve item in array at position matching button tag (array is 0 indexed)
NSDictionary *bundlesDataItem = [bundlesDataSource objectAtIndex:(subview.tag - 1)];
// Set button background to thumbnail of current bundle
NSString *picAddress = [NSString stringWithFormat:@"http://some.thing.com/data/images/%@/%@", [bundlesDataItem objectForKey:@"Nr"], [bundlesDataItem objectForKey:@"Thumb"]];
NSURL *picURL = [NSURL URLWithString:picAddress];
NSData *picData = [NSData dataWithContentsOfURL:picURL];
// Warning is generated here
[subview setBackgroundImage:[UIImage imageWithData:picData] forState:UIControlStateNormal];
}
}
I have a for loop setting the background images on buttons, basically the buttons are thumbnail previews of different items & can't be set statically, however the code gives an warning because it runs through all the UIViews but then calls setBackgroundImage which does not apply to all views. The warning is an irritation, I understand what it's complaining about, how can I get rid of it? (I don't want to turn off the warning, I want to fix the problem)
// For loop to set button images
for (UIView *subview in [self.view subviews]) // Loop through all subviews
{
// Look for the tagged buttons, only the 8 tagged buttons & within array bounds
if((subview.tag >= 1) && (subview.tag <= 8) && (subview.tag < totalBundles))
{
// Retrieve item in array at position matching button tag (array is 0 indexed)
NSDictionary *bundlesDataItem = [bundlesDataSource objectAtIndex:(subview.tag - 1)];
// Set button background to thumbnail of current bundle
NSString *picAddress = [NSString stringWithFormat:@"http://some.thing.com/data/images/%@/%@", [bundlesDataItem objectForKey:@"Nr"], [bundlesDataItem objectForKey:@"Thumb"]];
NSURL *picURL = [NSURL URLWithString:picAddress];
NSData *picData = [NSData dataWithContentsOfURL:picURL];
// Warning is generated here
[subview setBackgroundImage:[UIImage imageWithData:picData] forState:UIControlStateNormal];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
这样
id
类型将停止任何类型检查...或者检查对象是否响应选择器并像这样调用它:请注意,我从内存中编码了最后一部分。
You can either do this:
So that the
id
type will stop any type checking... or check whether the object responds to the selector and call it like this:Note that I coded that last part from memory.
带有如此多假设的危险代码,但是...在向 UIView 发送 setBackgroundImage 消息之前,您首先最好检查 UIView 的类,然后只需简单地转换 UIView 即可删除警告:
A dangerous bit of code with so many assumptions, but...you would first do well to check the class of the UIView before sending it a setBackgroundImage message and then just simply cast your UIView to remove the warning: