如何在UserDefaults和viewDidLoad中为Button添加.setTitle参数

时间:2017-10-24 09:30:57

标签: swift button swift4 userdefaults swiftyuserdefaults

也许有人知道如何在clearButton.setTitle中放置UserDefaults参数,然后将其覆盖func viewDidLoad() {

我尝试在viewDidLoad准备密钥之前尝试的参数,但我认为defaults.set不正确

let defaults = UserDefaults.standard

let clearB = "clearB"

clearButton.setTitle("Clear", for: .normal )

defaults.set(clearButton.setTitle, forKey: clearB).

感谢您的帮助 BR 乌拉

1 个答案:

答案 0 :(得分:0)

let clearB = "clearB"
clearButton.setTitle("Clear", for: .normal )

UserDefaults.standard.set(clearButton.titleLabel!.text, forKey: clearB)

提取:

if let title = UserDefaults.standard.string(forKey: clearB) {
    clearButton.setTitle(title, for: .normal)
}