Search code examples
iosswiftuitableviewuibuttonuserdefaults

UserDefaults UIButton States loads incorrectly sometimes


I am having trouble with UserDefaults loading the correct boolean value for a button state. Well I have two buttons within the same TableViewCell. One button is called "OzButton" and the other called "mLButton". Although I written the code and it has been working so far, sometimes there will be rare cases when I tap, for example, the mL button and then after relaunching the app, it would highlight the OzButton instead of the mL Button even though I already tapped it. I am creating the UIButtons and its own targets programmatically. I am not sure what I am doing wrong here. I will attach an image of the View Controller for visual purposes.

View Controller Image

I have been stuck on this for a while...

I am not sure what I am doing wrong... please help!

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
}

Solution

  • So after experimenting. It looks like it was a simulator issue (I was using the iPhone X sim). Running on a real device had no problem saving and updating UserDefaults.

    I used another simulator (iPhone 6s Plus) and still had NOT any issues with UserDefaults.

    Not sure why the iPhone X Sim was giving me issues...