简体   繁体   English

按钮的用户默认值

[英]UserDefaults for a Button

I am brand new here.我在这里是全新的。 I´ve been trying to research more info but not success, hopefully someone could help me with this:我一直在尝试研究更多信息但没有成功,希望有人可以帮助我:

I am building an app like a checkbox in one ViewController ( CLViewController ), and what I am using are buttons as an image and once pressed they change its display to another image.我正在一个 ViewController ( CLViewController ) 中构建一个类似于复选框的应用程序,我使用的是作为图像的按钮,一旦按下它们就会将其显示更改为另一个图像。 (Check/Unchecked -- item1a/item1b) (选中/未选中 -- item1a/item1b)

The app has other ViewControllers where you can have different information, but the problem that I have is the moment I go back to this CLViewController when I´m moving from another ViewControllers, the CLViewController returns to its default value which are all unchecked.该应用程序有其他 ViewControllers,您可以在其中拥有不同的信息,但我遇到的问题是当我从另一个 ViewControllers 移动时,我 go 回到这个CLViewController的那一刻, CLViewController返回到它的默认值,这些都是未选中的。 My intention is to keep the Checked buttons as they were when i left them.我的目的是让 Checked 按钮保持原样。 I know how to use userDefaults for texts but I don´t know how to code for buttons and its state.我知道如何将 userDefaults 用于文本,但我不知道如何为按钮及其 state 编写代码。

import UIKit


class CLviewControllerViewController: UIViewController {

    @IBOutlet weak var btn1: UIButton!
    @IBOutlet weak var btn2: UIButton!
    @IBOutlet weak var btn3: UIButton!




    @IBAction func btn1Action(_ sender: UIButton)
    {
        if sender.isSelected == true
        {
            btn1.setImage(UIImage (named: "item1a"), for: .normal)
            sender.isSelected = false
        }
        else
        {
            btn1.setImage(UIImage (named: "item1b"), for: .normal)
            sender.isSelected = true        
          }

First in viewDidLoad we will assign each button with a tag, btn1 will have tag 0, btn2 will have tag 1 and btn3 will have tag 2. Then we can read the value stored in UserDefaults and set the appropriate state.首先在 viewDidLoad 中,我们将为每个按钮分配一个标签, btn1将有标签 0, btn2将有标签 1, btn3将有标签 2。然后我们可以读取存储在UserDefaults中的值并设置适当的 state。 Note that we will store them for key Item- tag so btn3 will be stored as Item-2 .请注意,我们会将它们存储为关键Item- - tag ,因此 btn3 将存储为Item-2 Then in your @IBAction we store the state so that it can be retrieved when you get back to this view controller.然后在您的@IBAction中,我们存储 state 以便当您返回此视图 controller 时可以检索它。

@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
@IBOutlet weak var btn3: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()

    for (index, btn) in [btn1, btn2, btn3].enumerated() {
        btn?.tag = index

        let selected = UserDefaults.standard.bool(forKey: "Item-\(index)")
        btn?.isSelected = selected
    }

}

@IBAction func btn1Action(_ sender: UIButton)
{
    if sender.isSelected == true
    {
        btn1.setImage(UIImage (named: "item1a"), for: .normal)
        sender.isSelected = false
    }
    else
    {
        btn1.setImage(UIImage (named: "item1b"), for: .normal)
        sender.isSelected = true
    }
    let tag = sender.tag
    //save state
    UserDefaults.standard.set(sender.isSelected, forKey: "Item-\(tag)")
}

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

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