简体   繁体   English

userdefaults 不保存按钮状态

[英]userdefaults not saving button state

I am learning UserDefaults, and I am using UserDefaults to store button state in them and use them on re-launching application.我正在学习 UserDefaults,我正在使用 UserDefaults 在其中存储按钮状态并在重新启动应用程序时使用它们。

My viewController has two button on which click the button image is changed.我的 viewController 有两个按钮,单击按钮图像会更改。 Function which represents button targets:表示按钮目标的函数:

extension DetailViewController{
@objc func checkButtonTapped(sender: UIButton) {
    
   detailViewModel?.watchedButtonPressed(sender: sender)
   setupButton(sender: sender, button: .check)
}

@objc func favoriteButtonTapped(sender: UIButton) {
    
    detailViewModel?.favoriteButtonPressed(sender: sender)
    setupButton(sender: sender, button: .favorite)
}
}

Functions which are changing buttons states and save state value in UserDefault.更改按钮状态并在 UserDefault 中保存状态值的函数。

func watchedButtonPressed(sender: UIButton){
    
    sender.isSelected = !sender.isSelected
    UserDefaults.standard.setValue(sender.isSelected, forKey: "mvvmCheck\(movieIndex ?? 0)")  
}

func favoriteButtonPressed(sender: UIButton){

    sender.isSelected = !sender.isSelected
    UserDefaults.standard.setValue(sender.isSelected, forKey: "mvvmFavorite\(movieIndex ?? 0)") 
}

Function which change button image when its clicked:单击时更改按钮图像的功能:

func setupButton(sender: UIButton, button: buttonType){
    switch button {
    case .favorite:
        sender.setImage(UIImage(systemName: "star"), for: .normal)
        sender.setImage(UIImage(systemName: "star.fill"), for: .selected)
    case .check:
        sender.setImage(UIImage(systemName: "checkmark.seal"), for: .normal)
        sender.setImage(UIImage(systemName: "checkmark.seal.fill"), for: .selected)
    }
}

Function to setup buttons images when launching application:启动应用程序时设置按钮图像的功能:

func setupButtons(){

buttonChecked.isSelected = UserDefaults.standard.bool(forKey: "mvvmCheck\(detailViewModel?.movieIndex ?? 0)")
buttonFavorite.isSelected = UserDefaults.standard.bool(forKey: "mvvmFavorite\(detailViewModel?.movieIndex ?? 0)")
setupButton(sender: buttonChecked, button: .check)
setupButton(sender: buttonFavorite, button: .favorite)

}

But my values are not stored well when I am clicking (on app re-launch some buttons dont have valid picture (value) and some have) Should I use button tags to achieve this?但是当我点击时我的值没有很好地存储(在应用程序重新启动时,一些按钮没有有效的图片(值),而有些按钮有)我应该使用按钮标签来实现这一点吗? Is problem if I am sending sender value through functions calls?如果我通过函数调用发送发送值有问题吗?

Although I don't think you should be using UserDefaults to store this information the problem is the way you are setting the value to UserDefaults.尽管我认为您不应该使用 UserDefaults 来存储此信息,但问题在于您将值设置为 UserDefaults 的方式。

You are using您正在使用

UserDefaults.standard.setValue(...

If you command click on setValue in Xcode, you'll see that you are taken to the function in Foundation.NSKeyValueCoding .如果您命令单击 Xcode 中的setValue ,您将看到您被带到Foundation.NSKeyValueCoding的函数。 You should instead be using the correct UserDefaults function您应该使用正确的 UserDefaults 函数

UserDefaults.standard.set(sender.isSelected, ...

Which properly encodes the type.正确编码类型。

For set and get the value in user defaults:-对于设置和获取用户默认值中的值:-

class Constant {
   
    static func setValueInUserDefault<T>(value : T, key: String) {
        UserDefaults.standard.set(value, forKey: key)
        UserDefaults.standard.synchronize()
    }
    
    static func getValueInUserDefault<T>(key: String) -> T? {
        return UserDefaults.standard.value(forKey: key) as? T 
    }
    
}

You just need to call this class.你只需要调用这个类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM