Home > Software engineering >  How to get a button state to save after signing out and sign back in?
How to get a button state to save after signing out and sign back in?

Time:02-10

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.

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) {
        
    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) {
    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){
    quoteLabel.text = quote.quote
    
}  
}

CodePudding user response:

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
} 

CodePudding user response:

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.

  • Related