UIButton 设置代码生成的警告

发布于 2024-08-30 03:52:51 字数 1163 浏览 9 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最好是你 2024-09-06 03:52:51

您可以这样做:

for (id subview in [self.view subviews])

这样 id 类型将停止任何类型检查...或者检查对象是否响应选择器并像这样调用它:

if ([subview respondsToSelector:@selector(setBackgroundImage:forState:)]) {
    [subview performSelector:@selector(setBackgroundImage:forState:)
                  withObject:[UIImage imageWithData:picData]
                  withObject:UIControlStateNormal];
}

请注意,我从内存中编码了最后一部分。

You can either do this:

for (id subview in [self.view subviews])

So that the id type will stop any type checking... or check whether the object responds to the selector and call it like this:

if ([subview respondsToSelector:@selector(setBackgroundImage:forState:)]) {
    [subview performSelector:@selector(setBackgroundImage:forState:)
                  withObject:[UIImage imageWithData:picData]
                  withObject:UIControlStateNormal];
}

Note that I coded that last part from memory.

聆听风音 2024-09-06 03:52:51

带有如此多假设的危险代码,但是...在向 UIView 发送 setBackgroundImage 消息之前,您首先最好检查 UIView 的类,然后只需简单地转换 UIView 即可删除警告:

if ([subview class] == [UIButton class]) {
    [((UIButton *)subview) setBackgroundImage:[UIImage imageWithData:picData] forState:UIControlStateNormal];
}

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:

if ([subview class] == [UIButton class]) {
    [((UIButton *)subview) setBackgroundImage:[UIImage imageWithData:picData] forState:UIControlStateNormal];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文