<legend id='9Bhmo'><style id='9Bhmo'><dir id='9Bhmo'><q id='9Bhmo'></q></dir></style></legend>
      <i id='9Bhmo'><tr id='9Bhmo'><dt id='9Bhmo'><q id='9Bhmo'><span id='9Bhmo'><b id='9Bhmo'><form id='9Bhmo'><ins id='9Bhmo'></ins><ul id='9Bhmo'></ul><sub id='9Bhmo'></sub></form><legend id='9Bhmo'></legend><bdo id='9Bhmo'><pre id='9Bhmo'><center id='9Bhmo'></center></pre></bdo></b><th id='9Bhmo'></th></span></q></dt></tr></i><div id='9Bhmo'><tfoot id='9Bhmo'></tfoot><dl id='9Bhmo'><fieldset id='9Bhmo'></fieldset></dl></div>
        <bdo id='9Bhmo'></bdo><ul id='9Bhmo'></ul>
    1. <small id='9Bhmo'></small><noframes id='9Bhmo'>

      1. <tfoot id='9Bhmo'></tfoot>

        UserDefault 保存按钮状态

        时间:2023-07-07
          <tfoot id='zaVhi'></tfoot>

          <small id='zaVhi'></small><noframes id='zaVhi'>

          1. <i id='zaVhi'><tr id='zaVhi'><dt id='zaVhi'><q id='zaVhi'><span id='zaVhi'><b id='zaVhi'><form id='zaVhi'><ins id='zaVhi'></ins><ul id='zaVhi'></ul><sub id='zaVhi'></sub></form><legend id='zaVhi'></legend><bdo id='zaVhi'><pre id='zaVhi'><center id='zaVhi'></center></pre></bdo></b><th id='zaVhi'></th></span></q></dt></tr></i><div id='zaVhi'><tfoot id='zaVhi'></tfoot><dl id='zaVhi'><fieldset id='zaVhi'></fieldset></dl></div>
              <bdo id='zaVhi'></bdo><ul id='zaVhi'></ul>
              <legend id='zaVhi'><style id='zaVhi'><dir id='zaVhi'><q id='zaVhi'></q></dir></style></legend>

                    <tbody id='zaVhi'></tbody>

                • 本文介绍了UserDefault 保存按钮状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试通过保存状态来完成我最喜欢的按钮,即使我退出视图 App 也是如此.如果有人能告诉我我该怎么做就太好了这个,我使用 Xcode 8 和 Swift 3 编码.

                  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 Xcode 8 and coding with 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)
                  
                  
                          }
                      }
                  

                  推荐答案

                  可以将button中的代码缩减为

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

                  我不知道您的代码做了什么,但有很多冗余代码.IBAction 可以简化为(当然是未经测试的)

                  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.

                  这篇关于UserDefault 保存按钮状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:touchesBegin &amp;touchesMove Xcode Obj C 问题 下一篇:如何为 UIControl/UIButton 动画从一种状态到另一种状态的过渡?

                  相关文章

                    <bdo id='PLV6R'></bdo><ul id='PLV6R'></ul>

                  1. <small id='PLV6R'></small><noframes id='PLV6R'>

                  2. <tfoot id='PLV6R'></tfoot>

                      <legend id='PLV6R'><style id='PLV6R'><dir id='PLV6R'><q id='PLV6R'></q></dir></style></legend>
                      <i id='PLV6R'><tr id='PLV6R'><dt id='PLV6R'><q id='PLV6R'><span id='PLV6R'><b id='PLV6R'><form id='PLV6R'><ins id='PLV6R'></ins><ul id='PLV6R'></ul><sub id='PLV6R'></sub></form><legend id='PLV6R'></legend><bdo id='PLV6R'><pre id='PLV6R'><center id='PLV6R'></center></pre></bdo></b><th id='PLV6R'></th></span></q></dt></tr></i><div id='PLV6R'><tfoot id='PLV6R'></tfoot><dl id='PLV6R'><fieldset id='PLV6R'></fieldset></dl></div>