通过 UserDefault 设置更新子类 UIButton 的属性

Updating properties of a subclassed UIButton via UserDefault settings

我已经将 UIButton 子类化以在我的项目中使用。这些 UIButton 子类的边框颜色会根据用户 select 在设置中的内容而变化。使用 UserDefaults 保存设置。

问题: 当我第一次加载应用程序时,按钮边框是正确的颜色 - 但是,当我更改设置(最终更改按钮边框)时 - 没有任何反应。只有在我关闭应用程序并重新打开后,按钮才会改变颜色。

我的代码

class CustomSeasonButton: UIButton {
var seasonCold = Bool()
let nsDefaults = UserDefaults.standard
var notification = Notification(name: Notification.Name(rawValue: "themeChanged"))

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // ...
        self.seasonCold = nsDefaults.bool(forKey: "savedSeasonDefault")
        NotificationCenter.default.addObserver(self, selector: #selector(self.refreshButtonBorder), name: notification.name, object: nil)

        refreshButtonBorder()
    }

func refreshButtonBorder() {
    print("Notification Received")
    if seasonCold {
       seasonCold = false
       self.layer.borderColor = UIColor.blue
    }
    else {
       seasonCold = true
       self.layer.borderColor = UIColor.red
    }
}


}

设置

class SettingsVC: UITableViewController {
//...
    var notification = Notification(name: Notification.Name(rawValue: "themeChanged"))
//...
}

然后在本赛季的 IBAction select - 我有以下内容:

NotificationCenter.default.post(notification)

因此,正如您从我上面的代码中看到的那样,按钮边框颜色 应该 在蓝色和红色之间切换,具体取决于 select 使用 UserDefaults 编辑的内容。

要更改边框颜色的唯一方法是关闭应用程序并重新打开它。

问题: 如何确保 UIButton 子类在每次更新设置 (UserDefaults) 时都会更改边框颜色?谢谢!

要让您的按钮即时更改,他们必须知道设置已更改。他们将不得不收听某种广播消息,并在设置被修改时采取一些行动。一种方法可能是通过 NSNotificationCenter——创建时所有按钮都可以在默认通知中心上监听消息,然后当您注意到设置已更改时可以广播消息。

至于您上面的代码,请注意您是在 init 方法中设置边框颜色。当创建按钮时,该方法只被调用一次。这就解释了为什么你的按钮的边框颜色没有改变......它们的颜色是在创建时确定的。

最后,您可能有兴趣(如果您不知道)了解更多关于 [UIAppearance][1] 以及它允许​​您影响整组控件的颜色和样式的方式立刻。

seasonCold = false {
   didSet {
      refreshButtonBorder()
   }
}


func refreshButtonBorder() {
    if seasonCold {
       self.layer.borderColor = UIColor.blue
    }
    else {
       self.layer.borderColor = UIColor.red
    }
}

并在您更改用户默认值时设置 seasonCold "savedSeasonDefault"

对于一对多调用使用 NotificationCenter,子类按钮的每个实例都应该有一个 NotificationObserver,并且 post 每当 UserDefaults 更改时通知

var notification = Notification(name: Notification.Name(rawValue: "seasonChanged"))

从 nib 或您的初始化函数中唤醒

NotificationCenter.default.addObserver(self, selector: #selector(self.refreshButtonBorder), name: notification.name, object: nil)

每当季节寒冷变化洒通知灰尘

NotificationCenter.default.post(notification)