UserDefault To Save Button State

后端 未结 2 541
自闭症患者
自闭症患者 2021-01-07 08:03

I\'m trying to complete my favorite button by saving it state even when i quit the view \\ App . it will be great if anyone could show me how can i do this, I\'m using

相关标签:
2条回答
  • 2021-01-07 08:13

    You can reduce the code in button to

    @IBAction func button(sender: UIButton) {
    
        sender.isSelected = !sender.isSelected
        UserDefaults.standard.set(sender.isSelected, forKey: "isSaved")
    }
    

    To set the state you have to read it back

    let Favoritebutton = UIButton(type: UIButtonType.custom)
    //set image for button
    Favoritebutton.setImage(UIImage(named: "EmptyHeart.png"), for: .normal)
    Favoritebutton.setImage(UIImage(named: "FilledHeart.png"), for: .selected)
    Favoritebutton.isSelected = UserDefaults.standard.bool(forKey: "isSaved")
    ...
    

    Edit:

    I have no clue what your code does but there is a lot of redundant code. The IBAction can be reduced to (untested, of course)

    @IBAction func button(sender: UIButton) {
    
        let newValue = !sender.isSelected
        sender.isSelected = newValue
        UserDefaults.standard.set(newValue, forKey: "isSaved")
    
        let tabItem = self.tabBarController?.tabBar.items![3]
        sel_val = tabItem?.badgeValue
        if(sel_val == nil){
            sel_val = "0"
        }
        let sel_num  = Int(sel_val!)
    
        let fav: NSMutableArray = []
        fav.add(barImage)
        fav.add(barName)
        fav.add(streetName)
        if sender.isSelected {
            tabItem!.badgeValue = String(format: "%d", sel_num! + 1)
            favorite.add(fav)
        } else {
            tabItem!.badgeValue = String(format: "%d", sel_num! - 1)
            favorite.remove(fav)
        }
    }
    

    and please name variables with a starting lowercase letter.

    0 讨论(0)
  • 2021-01-07 08:30

    Every time you use UserDefaults.standard.set you need sync , add UserDefaults.standard.synchronize().

    To remove/delete a saved value use UserDefaults.standard.removeObject(forKey: "YOUR_KEY")

    here is your func

    @IBAction func button(sender: UIButton) {
    
        sender.isSelected = !sender.isSelected
    
    
        //one thing i tried , doesn't work :
        UserDefaults.standard.set(true,forKey: "isSaved")
        UserDefaults.standard.synchronize()
    
    
        if let Favoritebutton = sender as? UIButton {
            if Favoritebutton.isSelected {
                // set selected
                Favoritebutton.isSelected = true
                UserDefaults.standard.set(true, forKey: "MY_FAV_KEY")
                UserDefaults.standard.synchronize()
                //Add to favorites
    
            } else {
                // set deselected
                Favoritebutton.isSelected = false
                UserDefaults.standard.set(false, forKey: "MY_FAV_KEY")
                UserDefaults.standard.synchronize()
                //remove from Favorites
    
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题