UserDefaults UIButton 状态有时加载不正确
UserDefaults UIButton States loads incorrectly sometimes
我在使用 UserDefaults 为按钮状态加载正确的布尔值时遇到问题。好吧,我在同一个 TableViewCell 中有两个按钮。一个按钮称为 "OzButton",另一个称为 "mLButton"。虽然我编写了代码并且到目前为止它一直在工作,但有时会出现罕见的情况,例如,当我点击 mL 按钮然后在重新启动应用程序后,它会突出显示 OzButton 而不是 mL 按钮,即使我已经轻拍它。我正在以编程方式创建 UIButtons 及其自己的目标。我不确定我在这里做错了什么。为了视觉目的,我将附上 View Controller 的图像。
View Controller Image
我已经卡在这个问题上一段时间了...
我不确定我做错了什么...请帮忙!
var didOzTapped = Bool()
let conversionDefaults = UserDefaults.standard
lazy var ozButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("oz", for: .normal)
button.setTitleColor(.black, for: .normal)
button.tintColor = .clear
button.layer.cornerRadius = 15
button.addTarget(self, action: #selector(handleOzTap), for: .touchUpInside)
return button
}()
lazy var mlButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("mL", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.contentMode = .center
button.titleLabel?.font = UIFont(name: "SFProDisplay-Bold", size: 16)
button.backgroundColor = .white
button.addTarget(self, action: #selector(handleMLTap), for: .touchUpInside)
return button
}()
override func viewWillAppear(_ animated: Bool) {
checkForConversionDefaults()
}
@objc func handleOzTap() {
didOzTapped = true
mlButton.isSelected = !didOzTapped
ozButton.isEnabled = false
mlButton.isEnabled = true
ozButton.backgroundColor = .blue
mlButton.backgroundColor = .white
conversionDefaults.set(didOzTapped, forKey: "tapTheOz")
}
@objc func handleMLTap() {
didOzTapped = false
ozButton.isSelected = didOzTapped
ozButton.isEnabled = true
mlButton.isEnabled = false
mlButton.backgroundColor = .blue
ozButton.backgroundColor = .white
conversionDefaults.set(didOzTapped, forKey: "tapTheOz")
}
fileprivate func checkForConversionDefaults() {
if conversionDefaults.bool(forKey: "tapTheOz") {
print("ConversionDefaults: true")
ozButton.backgroundColor = .blue
mlButton.backgroundColor = .white
} else {
print("ConversionDefaults: false")
mlButton.backgroundColor = .blue
ozButton.backgroundColor = .white
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let profileCell = ProfileCell(style: .default, reuseIdentifier: nil)
switch indexPath.row {
case 0:
case 1:
// some code
case 2:
profileCell.cellView.addSubview(measurementLabel)
measurementUnitLabel.anchor(top: profileCell.cellView.topAnchor, leading: profileCell.cellView.leadingAnchor, bottom: .none, trailing: profileCell.cellView.trailingAnchor, padding: .init(top: 5, left: 20, bottom: 0, right: 20))
// created a stackView called ozMilButtonStackView (ozButton and mLButton)
profileCell.cellView.addSubview(ozMilButtonStackView)
ozMilButtonStackView.anchor(top: measurementUnitLabel.bottomAnchor, leading: profileCell.cellView.leadingAnchor, bottom: profileCell.cellView.bottomAnchor, trailing: profileCell.cellView.trailingAnchor, padding: .init(top: 0, left: 100, bottom: 10, right: 100))
default:
break
}
return profileCell
}
在 UserDefaults 中设置值后添加此 conversionDefaults.synchronize()
。
代码:
@objc func handleOzTap() {
didOzTapped = true
mlButton.isSelected = !didOzTapped
ozButton.isEnabled = false
mlButton.isEnabled = true
ozButton.backgroundColor = .blue
mlButton.backgroundColor = .white
conversionDefaults.set(didOzTapped, forKey: "tapTheOz")
conversionDefaults.synchronize()
}
所以经过实验。看起来这是一个模拟器问题(我使用的是 iPhone X sim)。 运行 在真实设备上保存和更新 UserDefaults 没有问题。
我使用了另一个模拟器 (iPhone 6s Plus),UserDefaults 仍然没有任何问题。
不确定为什么 iPhone X Sim 给我问题...
我在使用 UserDefaults 为按钮状态加载正确的布尔值时遇到问题。好吧,我在同一个 TableViewCell 中有两个按钮。一个按钮称为 "OzButton",另一个称为 "mLButton"。虽然我编写了代码并且到目前为止它一直在工作,但有时会出现罕见的情况,例如,当我点击 mL 按钮然后在重新启动应用程序后,它会突出显示 OzButton 而不是 mL 按钮,即使我已经轻拍它。我正在以编程方式创建 UIButtons 及其自己的目标。我不确定我在这里做错了什么。为了视觉目的,我将附上 View Controller 的图像。
View Controller Image
我已经卡在这个问题上一段时间了...
我不确定我做错了什么...请帮忙!
var didOzTapped = Bool()
let conversionDefaults = UserDefaults.standard
lazy var ozButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("oz", for: .normal)
button.setTitleColor(.black, for: .normal)
button.tintColor = .clear
button.layer.cornerRadius = 15
button.addTarget(self, action: #selector(handleOzTap), for: .touchUpInside)
return button
}()
lazy var mlButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("mL", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.contentMode = .center
button.titleLabel?.font = UIFont(name: "SFProDisplay-Bold", size: 16)
button.backgroundColor = .white
button.addTarget(self, action: #selector(handleMLTap), for: .touchUpInside)
return button
}()
override func viewWillAppear(_ animated: Bool) {
checkForConversionDefaults()
}
@objc func handleOzTap() {
didOzTapped = true
mlButton.isSelected = !didOzTapped
ozButton.isEnabled = false
mlButton.isEnabled = true
ozButton.backgroundColor = .blue
mlButton.backgroundColor = .white
conversionDefaults.set(didOzTapped, forKey: "tapTheOz")
}
@objc func handleMLTap() {
didOzTapped = false
ozButton.isSelected = didOzTapped
ozButton.isEnabled = true
mlButton.isEnabled = false
mlButton.backgroundColor = .blue
ozButton.backgroundColor = .white
conversionDefaults.set(didOzTapped, forKey: "tapTheOz")
}
fileprivate func checkForConversionDefaults() {
if conversionDefaults.bool(forKey: "tapTheOz") {
print("ConversionDefaults: true")
ozButton.backgroundColor = .blue
mlButton.backgroundColor = .white
} else {
print("ConversionDefaults: false")
mlButton.backgroundColor = .blue
ozButton.backgroundColor = .white
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let profileCell = ProfileCell(style: .default, reuseIdentifier: nil)
switch indexPath.row {
case 0:
case 1:
// some code
case 2:
profileCell.cellView.addSubview(measurementLabel)
measurementUnitLabel.anchor(top: profileCell.cellView.topAnchor, leading: profileCell.cellView.leadingAnchor, bottom: .none, trailing: profileCell.cellView.trailingAnchor, padding: .init(top: 5, left: 20, bottom: 0, right: 20))
// created a stackView called ozMilButtonStackView (ozButton and mLButton)
profileCell.cellView.addSubview(ozMilButtonStackView)
ozMilButtonStackView.anchor(top: measurementUnitLabel.bottomAnchor, leading: profileCell.cellView.leadingAnchor, bottom: profileCell.cellView.bottomAnchor, trailing: profileCell.cellView.trailingAnchor, padding: .init(top: 0, left: 100, bottom: 10, right: 100))
default:
break
}
return profileCell
}
在 UserDefaults 中设置值后添加此 conversionDefaults.synchronize()
。
代码:
@objc func handleOzTap() {
didOzTapped = true
mlButton.isSelected = !didOzTapped
ozButton.isEnabled = false
mlButton.isEnabled = true
ozButton.backgroundColor = .blue
mlButton.backgroundColor = .white
conversionDefaults.set(didOzTapped, forKey: "tapTheOz")
conversionDefaults.synchronize()
}
所以经过实验。看起来这是一个模拟器问题(我使用的是 iPhone X sim)。 运行 在真实设备上保存和更新 UserDefaults 没有问题。
我使用了另一个模拟器 (iPhone 6s Plus),UserDefaults 仍然没有任何问题。
不确定为什么 iPhone X Sim 给我问题...