【问题标题】:Override UI element .enabled property in Swift在 Swift 中覆盖 UI 元素 .enabled 属性
【发布时间】:2016-12-14 22:05:47
【问题描述】:

我想覆盖UIView 类的属性,以便在每次更改 .enabled 的属性值时执行额外的代码行。我该怎么做?

例如:

  1. MyUIViewclass myClass
  2. 有 myClass.enabled 属性
  3. 我想覆盖它来添加下一个东西
    • 编辑 UIView 类型的子视图,如果启用则将背景设置为红色/如果禁用则将背景设置为绿色。
    • 编辑UIButton 类型的子视图并在禁用时禁用/启用时启用。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    代码看起来像这样 -

        class SubClass: UIView {
            override var userInteractionEnabled: Bool {
                didSet {
                    // Do the color change and other stuff. 
                    // Use oldValue to access old value 
                }
            }
        }
    

    关于这个 setter 和 getter 有更多的探索。

    【讨论】:

    • 这正是我需要做的。谢谢!
    猜你喜欢
    • 2014-08-18
    • 2011-09-29
    • 2018-06-09
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 2014-12-28
    • 2014-02-21
    • 2015-02-20
    相关资源
    最近更新 更多