【问题标题】:UserDefaults UIButton States loads incorrectly sometimesUserDefaults UIButton 状态有时加载不正确
【发布时间】:2020-03-19 04:19:51
【问题描述】:

我在 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()
  • 这可能是模拟器的默认问题。 UISegmentedControl 夜间工作比两个按钮更好。如果这是一个真实的应用程序,您还应该测试可访问性;确保有各种视力障碍的人能够分辨选择了哪个按钮。画外音也一样
  • 感谢您对 Paulw11 的回复!在真实设备上使用它一直是一致和正确的......到目前为止。在模拟器上运行时,它可以完成这项工作,但在那些罕见的情况下,UserDefaults 在加载正确的值时会很奇怪......我一定会尝试 UISegmentedControl 方式

标签: ios swift uitableview uibutton userdefaults


【解决方案1】:

在 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()

}

【讨论】:

  • 所以我添加了那行代码。重新运行应用程序,它被设置为 mL 按钮,我点击了 Oz 按钮。再次重新启动应用程序,它被设置回 mL 按钮......仅供参考,我正在使用 iPhone X 模拟器。我也尝试使用我的真实设备,它一直是一致且正确的......但我担心它是否不会保持一致哈哈不确定这是否是模拟器问题。
  • @georrgee 不知道,在我的模拟器中工作正常,您能否检查 UserDefaults 中的值是否错误或您的 UI 配置不正确,这意味着在重新启动时检查 UserDefaults 的值。
  • 多么奇怪……我一直在使用调试器和打印语句。一切都有正确的值...即使在加载控制器时,UserDefaults 确实包含正确的布尔值...再次,有时它会设置回以前的状态。我不确定我的逻辑是否不正确......我已经更新了关于如何设置 UI 的问题和代码。在 cellForRow 函数中设置视图是不好的做法还是没关系?
  • 在单元格内添加 mlButton 和 ozButton 而不是添加。
  • 我没有对您投反对票 SGDev。也许其他人做了
【解决方案2】:

所以在实验之后。看起来这是一个模拟器问题(我使用的是 iPhone X sim)。在真实设备上运行保存和更新 UserDefaults 没有问题。

我使用了另一个模拟器(iPhone 6s Plus),但 UserDefaults 仍然没有任何问题。

不知道为什么 iPhone X Sim 会给我带来问题...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2022-10-06
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多