【发布时间】:2020-03-19 04:19:51
【问题描述】:
我在 UserDefaults 加载按钮状态的正确布尔值时遇到问题。好吧,我在同一个 TableViewCell 中有两个按钮。一个按钮称为“OzButton”,另一个称为“mLButton”。虽然我编写了代码并且到目前为止它一直在工作,但有时会出现一些罕见的情况,例如,当我点击 mL 按钮,然后在重新启动应用程序后,它会突出显示 OzButton 而不是 mL 按钮,即使我已经轻拍它。我正在以编程方式创建 UIButtons 及其自己的目标。我不确定我在这里做错了什么。出于视觉目的,我将附上 View Controller 的图像。
我已经被困了一段时间了......
我不确定我做错了什么...请帮助!
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() -
这可能是模拟器的默认问题。
UISegmentedControl夜间工作比两个按钮更好。如果这是一个真实的应用程序,您还应该测试可访问性;确保有各种视力障碍的人能够分辨选择了哪个按钮。画外音也一样 -
感谢您对 Paulw11 的回复!在真实设备上使用它一直是一致和正确的......到目前为止。在模拟器上运行时,它可以完成这项工作,但在那些罕见的情况下,UserDefaults 在加载正确的值时会很奇怪......我一定会尝试 UISegmentedControl 方式
标签: ios swift uitableview uibutton userdefaults