【问题标题】:What is the UIButton backgroundImage key? iOSUIButton backgroundImage 键是什么? iOS
【发布时间】:2012-07-20 15:14:35
【问题描述】:

这是我的第一个问题。

我正在尝试以编程方式更改UIButton 的背景图像,但有一个问题,我找不到背景图像的键。 为了理解,这是我的代码:

    [[self.view viewWithTag:69000+i] setValue:[UIImage imageNamed:[NSString stringWithFormat:@"%d-options.png", i+1]] forKey:@"backgroundImage"];

执行此操作时,应用程序崩溃。给出这个错误:

线程 1:信号 SIGTRAP 在 main.m
并在调试器中:(lldb)

那么,如果我是对的并且崩溃是由不正确的键(backgroundImage)引起的,那么引用背景图像的键是什么?

非常感谢!!

PD:我需要 self.view viewWithTag 来完成......

【问题讨论】:

    标签: ios xcode image uibutton setvalue


    【解决方案1】:

    你为什么不使用这个:

       if([[self.view viewWithTag:69000+i] isKindOfClass:[UIButton class]])
        {
            UIButton *currentButton = (UIButton *)[self.view viewWithTag:69000+i];
            [currentButton setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d-options.png", i+1]] forState:UIControlStateNormal];
        }
    

    【讨论】:

    • +1 我想要 backgroundImage,所以我更正了它,它起作用了!我不知道 isKindOfClass 方法。尽管按我的方式更改背景图像的正确键是什么?谢谢!
    • 你不能通过将它分配给一个键来设置它。它不是字典。只能使用setImage:forState:setBackgroundImage:forState: 来设置UIButtonimagebackgroundImage 属性。
    • @JustinPaulson 明白了。谢谢!
    猜你喜欢
    • 2011-08-11
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    相关资源
    最近更新 更多