UserDefault保存按钮状态

时间:2017-04-15 18:23:59

标签: ios swift xcode uibutton nsuserdefaults

我正在尝试通过保存状态来完成我最喜欢的按钮,即使我退出了视图\ App。如果有人能告诉我我该怎么办,那将是很棒的 我正在使用Xcode 8并使用Swift 3进行编码。

  //create a new button
    let Favoritebutton: UIButton = UIButton(type: UIButtonType.custom)
    //set image for button
    Favoritebutton.setImage(UIImage(named: "EmptyHeart.png"), for: .normal)
    Favoritebutton.setImage(UIImage(named: "FilledHeart.png"), for: .selected)
    //add function for button
    Favoritebutton.addTarget(self, action: #selector(self.button), for: .touchUpInside)
    //set frame
    Favoritebutton.frame = CGRect(x:0,y: 0,width: 35,height: 35)

    Favoritebutton.isSelected = UserDefaults.standard.bool(forKey: "isSaved")

    let barButton = UIBarButtonItem(customView: Favoritebutton)
    //assign button to navigationbar

    self.navigationItem.rightBarButtonItem = barButton

    let state = UserDefaults.standard.bool(forKey: "isSaved") ?? false

    }


@IBAction func button(sender: UIButton) {

    sender.isSelected = !sender.isSelected
    if let Favoritebutton = sender as? UIButton {
        Favoritebutton.isSelected = UserDefaults.standard.bool(forKey: "isSaved")
        if Favoritebutton.isSelected {
            // set selected
            Favoritebutton.isSelected = true



    // set badge Value to tabbar item.
    let tabItem = self.tabBarController?.tabBar.items![3]
    sel_val = tabItem?.badgeValue
    if(sel_val == nil){
        sel_val = "0"
    }
    let sel_num  = Int(sel_val!)
    tabItem!.badgeValue = String(format: "%d", sel_num! + 1) as String
    //Add Favorite
    let Fav: NSMutableArray = []
    Fav.add(barImage)
    Fav.add(barName)
    Fav.add(streetName)
    favorite.add(Fav)




        } else {
            // set deselected
            Favoritebutton.isSelected = false
            //Badge Value Count.
            let tabItem = self.tabBarController?.tabBar.items![3]
            sel_val = tabItem?.badgeValue
            if(sel_val == nil){
                sel_val = "0"
            }
            let sel_num  = Int(sel_val!)
            tabItem!.badgeValue = String(format: "%d", sel_num! - 1) as String
            //Remove Favorite
            let Fav: NSMutableArray = []
            Fav.add(barImage)
            Fav.add(barName)
            Fav.add(streetName)
            favorite.remove(Fav)


        }
    }

2 个答案:

答案 0 :(得分:5)

您可以将button中的代码缩减为

@IBAction func button(sender: UIButton) {

    sender.isSelected = !sender.isSelected
    UserDefaults.standard.set(sender.isSelected, forKey: "isSaved")
}

要设置状态,您必须将其读回

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")
...

编辑:

我不知道你的代码是做什么的,但是有很多冗余代码。 IBAction可以缩减为(当然未经测试)

@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)
    }
}

请用起始小写字母命名变量。

答案 1 :(得分:0)

每次使用UserDefaults.standard.set时都需要同步,请添加UserDefaults.standard.synchronize()

要删除/删除已保存的值,请使用UserDefaults.standard.removeObject(forKey: "YOUR_KEY")

这是你的功能

@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

        }
    }
}