【问题标题】:Background color背景颜色
【发布时间】:2016-04-07 00:28:36
【问题描述】:

这是一个简单的问题,但我一直在寻找答案,但没有运气。

我想访问按钮的背景颜色以将其保存到变量中,以便以后访问。

所以,我在视图控制器中声明了一个实例属性:

@property UIColor *variable;

并且,在我的方法中:

-(IBAction)buttonPressed:(id)sender {
    variable = sender.backgroundColor;
}

但我得到了错误:

在 _strong id 警告类型的对象中找不到属性 backgroundcolor。

哪里出错了?

【问题讨论】:

  • -(IBAction)button pressed:(sender:UIButton) 我认为背景颜色属性是基于发件人 UIButton.check 与那个
  • [button.backgroundColor = sender.backgroundColor];也不工作

标签: objective-c uibutton background-color


【解决方案1】:

您有三个选择:

  1. sender 属性从id 更改为UIButton *

    - (IBAction)buttonPressed:(UIButton *)sender {
        self.variable = sender.backgroundColor;
    }
    
  2. 调用backgroundColor getter 方法而不是使用backgroundColor 属性。

    - (IBAction)buttonPressed:(id)sender {
        self.variable = [sender backgroundColor];
    }
    
  3. sender 上使用演员表。

    - (IBAction)buttonPressed:(id)sender {
        self.variable = ((UIButton *)sender).backgroundColor;
    }
    

您遇到的主要问题是您不能对 id 类型的对象使用属性引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2015-11-29
    • 1970-01-01
    相关资源
    最近更新 更多