如何在注销并重新登录后保存按钮状态



我有一个保存按钮,当点击该按钮时,将把标签(quoteLabel(中的文本保存到Firebase中的用户帐户。按钮将隐藏,取消保存按钮将不再隐藏,以便用户可以根据需要取消保存。这两个按钮都可以根据需要发布和删除数据,但是,如果我注销应用程序并重新登录,按钮将重置为原始状态。如何使按钮保持上次设置的状态?我似乎无法让它发挥作用。如果需要更多信息,请告诉我,我对这一切还很陌生。

编辑:更新了以下建议。我似乎仍然无法正常工作。当我只点击一个按钮时,当我重新登录时,它们都会被保存为点击。我还添加了如何分配密钥。

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

}
}

这就是我试图为每个报价添加一个密钥的方式:

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),
]

问题是UIButton的.isHidden属性是一个状态变量,这意味着它保存在RAM中。这意味着,一旦您退出应用程序并重新打开它,您的应用程序将使用默认状态变量设置重新启动
如果您希望保持这些变量的状态,则需要将按钮状态保存到手机的硬盘驱动器中。UserDefaults使此操作变得简单。

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

您需要将信息存储在本地设备上。最好的方法是根据用户信息将信息存储在firebase上。我认为在这里显示代码没有问题。

最新更新