'How to get a button state to save after signing out and sign back in?

I have a save button that when tapped, will save the text in a label (quoteLabel) to a user account in Firebase. The button will then hide and the unsave button will no longer be hidden so that the user can unsave if desired. Both of these buttons are able to post and delete data as desired, however, if I sign-out of the app and sign back in, the buttons are reset to their original status. How do I get the button to stay in the last state it was set to? I can't seem to get this to work. Please let me know if more information is needed, I'm still new to all this.

Edit: Updated with suggestions from below. I still can't seem to get this to work properly. When I tap just one button, they all end up being saved as tapped when I sign back in. I added how I assigned a key as well.

import Foundation
import UIKit
import FirebaseDatabase
import FirebaseAuth

class QuotesCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var quoteLabel: UILabel!
@IBOutlet weak var save: UIButton!
@IBOutlet weak var unsave: UIButton!
@IBAction func saveButton(_ sender: UIButton) {
UserDefaults.standard.set(true, forKey: uuid)

save.isHidden = true
unsave.isHidden = false

var ref: DatabaseReference?
ref = Database.database().reference()

if quoteLabel.text == "The best time to plant a tree was 20 years ago - The next best time is today - Unknown"{

guard let user = Auth.auth().currentUser?.uid else { return }
ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quote").child("Quote1").setValue(quoteLabel.text!)
    
}

}

@IBAction func UnsaveButton(_ sender: UIButton) {
UserDefaults.standard.set(false, forKey: uuid)


save.isHidden = false
unsave.isHidden = true

var ref: DatabaseReference?
ref = Database.database().reference()

if quoteLabel.text == "The best time to plant a tree was 20 years ago - The next best time is today - Unknown"{
ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quote").child("Quote1").removeValue()

}


}

func setup(with quote: Quotes){
let saveButtonShowing = UserDefaults.standard.bool(forKey: uuid)
if(saveButtonShowing){ // Retrieves the state variable from the hard drive and sets the button visibility accordingly
    save.isHidden = true
    unsave.isHidden = false
} else {
    save.isHidden = false
    unsave.isHidden = true
}

quoteLabel.text = quote.quote


}
}

This is how I tried to add a key to each quote:

import UIKit
import Foundation


let uuid = UUID().uuidString


struct Quotes {
let quote: String
let identifier: String

}

let quotes: [Quotes] = [
Quotes(quote: "The best time to plant a tree was 20 years ago - The next best time is today - Unknown", identifier:  uuid),
                        Quotes(quote:    "Everytime you spend money, you're casting a vote for the type of world you want - Anna Lappe",identifier: uuid),
                        Quotes(quote: "Buy less, choose well, make it last - Vivienne Westwood", identifier: uuid),
                        Quotes(quote: "The future depends on what we do in the present - Mahatma Gandhi", identifier: uuid),
]


Solution 1:[1]

The problem is that the .isHidden property of the UIButton's is a state variable, meaning it is saved in RAM. This means that once you exit the application and reopen it, your app will startup again with the default state variable settings.
If you wish to persist the state of these variables, you'll need to save the button states to the phone's hard drive. UserDefaults makes this easy.

@IBAction func saveButton(_ sender: UIButton) {
    UserDefaults.standard.set(true, forKey: "saveButtonHidden") // Saves the state to the hard drive

    save.isHidden = true
    unsave.isHidden = false

    var ref: DatabaseReference?
    ref = Database.database().reference()

    if quoteLabel.text == "The best time to plant a tree was 20 years ago - The next best time is today - Unknown"{
        guard let user = Auth.auth().currentUser?.uid else { return }
        ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quote").chi.ld("Quote1").setValue(quoteLabel.text!)
    }

}


@IBAction func UnsaveButton(_ sender: UIButton) {
    UserDefaults.standard.set(false, forKey: "saveButtonHidden") // Saves the state to the hard drive

    save.isHidden = false
    unsave.isHidden = true

    var ref: DatabaseReference?
    ref = Database.database().reference()

    if quoteLabel.text == "The best time to plant a tree was 20 years ago - The next best time is today - Unknown"{
        ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quote").child("Quote1").removeValue()
    }

    if quoteLabel.text as! NSObject == ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quote").child("Quote1") {
        save.isHidden = true
        unsave.isHidden = false
    }
}


func setup(with quote: Quotes){
    let saveButtonShowing = UserDefault.standard.bool(forKey: "saveButtonHidden")
    if(saveButtonHidden){ // Retrieves the state variable from the hard drive and sets the button visibility accordingly
        save.isHidden = true
        unsave.isHidden = false
    } else {
        save.isHidden = false
        unsave.isHidden = true
    }
    
    quoteLabel.text = quote.quote
} 

Solution 2:[2]

You need to store the info on local device. the best way is to store the info on firebase according to the user info. I think it's no problem to show the code here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Venus