gpt4 book ai didi

ios - 第一次按按钮不起作用,但第二次就可以了

转载 作者:行者123 更新时间:2023-11-30 12:34:12 25 4
gpt4 key购买 nike

我对整体编码还很陌生,正在学习 swift 3,并且我正在尝试集成一个按下时会“加星”的按钮。因此,目前它会向 Firebase 写入状态“true”,以及按下时的更多数据。

问题是它只能在按下两次时起作用。但在那之后它就完美地工作了......(编辑:“不起作用”我的意思是,我第一次按下时它不会将图像更新为“填充64x64”。我刚刚 checkin Firebase,它似乎在我第一次按下时创建了一条记录,但是将其设为最爱= false)

一些更有经验的编码员可以帮助我了解问题所在吗?

这是“星号”按钮的代码:

/*
==========================================================================================
//MARK: Start of code that stars your whisky Firebase
==========================================================================================
*/
var isStarred = UserDefaults.standard.bool(forKey: "isStarred")
@IBAction func starButton(_ sender: Any) {
//loads info for query
let currentUserID = self.user?.uid
let realIndex:Int? = detailWhisky?.realIndex
let realIndex2 = "\(realIndex!)"
//executes query
let starRef2 = FIRDatabase.database().reference().child("Starred").child(currentUserID!).child(realIndex2)
//print("booyah")
//changes state of button and loads true/false to Firebase
if isStarred == true {
let image = UIImage(named: "star64x64.png")
(sender as AnyObject).setImage(image, for: UIControlState.normal)
let favourite = ["favourite": false, "realIndex": detailWhisky?.realIndex, "shortName": self.title, "distillery": distilleryLabel.text] as [String : Any]
starRef2.updateChildValues(favourite, withCompletionBlock: { (err, ref) in
if err != nil {
print(err as Any)
return
} })}
else {
let image = UIImage(named: "filled 64x64.png")
(sender as AnyObject).setImage(image, for: UIControlState.highlighted)
let favourite = ["favourite": true, "realIndex": detailWhisky?.realIndex, "shortName": self.title, "distillery": distilleryLabel.text] as [String : Any]
starRef2.updateChildValues(favourite, withCompletionBlock: { (err, ref) in
if err != nil {
print(err as Any)
return
} })}
isStarred = !isStarred
UserDefaults.standard.set(isStarred, forKey: "isStarred")
UserDefaults.standard.synchronize()
}

然后为了填充,我在 ViewDidLoad 中运行“starred()”。

override func viewDidLoad() {
super.viewDidLoad()

configureView() //populates text fields etc.
starred() // populates the starred star


}

“starred()”函数如下所示:

//func starred(likeButton: UIButton) {
func starred() {
//initalize to make query work
let currentUserID = self.user?.uid //to get userID
let realIndex:Int? = detailWhisky?.realIndex
let realIndex2 = "\(realIndex!)" //realIndex
//query to firebase
starRef.child(currentUserID!).queryOrdered(byChild: "realIndex").queryEqual(toValue: realIndex).observe(.value, with: { snapshot in
if snapshot.value is NSNull {
print("Ingen treff i stjaernesoeket")
} else {
for child in snapshot.children {
let newStarred = Favourite(snapshot: child as! FIRDataSnapshot)
self.detailStarred = newStarred
//print(newStarred)

var boolStorage: Bool? = self.detailStarred?.favourite
if let boolStorage = boolStorage, boolStorage {
// executes when booleanValue is true
print("unwrapped boolStorage: '\(boolStorage)'")
let myImage = UIImage(named: "filled 64x64.png")
self.starButton.setImage(myImage, for: UIControlState.normal)
}
else {
print("not a favourite")}

}}})
}

数据结构如下所示:

struct Favourite {

var key: String?
let ref: FIRDatabaseReference?
var favourite: Bool?
var currentUserID: String?
var realIndex: Int?
var shortName: String?
let distillery: String?

init(key: String = "", favourite: Bool, currentUserID: String, realIndex: Int, shortName: String, distillery: String){
self.key = key
self.ref = nil
self.favourite = favourite
self.currentUserID = currentUserID
self.realIndex = realIndex
self.shortName = shortName
self.distillery = distillery

}


init(snapshot: FIRDataSnapshot) {
key = snapshot.key
ref = snapshot.ref
let snapshotValue = snapshot.value as! [String: AnyObject]
favourite = snapshotValue["favourite"] as? Bool
currentUserID = snapshotValue["currentUserID"] as? String
realIndex = snapshotValue["realIndex"] as? Int
shortName = snapshotValue["shortName"] as? String
distillery = snapshotValue["distillery"] as? String
}
func toAnyObject() -> Any {
return [
"favourite": favourite as Any,
"currentUserID": currentUserID as Any,
"realIndex": realIndex as Any,
"shortName": shortName as Any,
"distillery": distillery as Any,
]
}

}

最佳答案

我找到了适合我的解决方案。我确信可以更优雅地处理它,但我将其发布,以防它可以帮助其他人。

解决方案是添加一个额外的变量“buttonStarred”,如果之前未收藏该变量,该变量将从 Firebase 获取值。然后,当按下“加星标的按钮”时,我首先检查此变量,然后再继续现有代码。

从 Firebase 加载收藏夹状态的函数。现在它为“buttonStarred”变量提供了“missing”值。

func starred() {
//initalize to make query work
let currentUserID = self.user?.uid //to get userID
let realIndex:Int? = detailWhisky?.realIndex

//query to firebase
starRef.child(currentUserID!).queryOrdered(byChild: "realIndex").queryEqual(toValue: realIndex).observe(.value, with: { snapshot in
if snapshot.value is NSNull {
print("Ingen treff i stjaernesoeket")
self.buttonStarred = "missing"
print(self.buttonStarred as Any)
} else { for child in snapshot.children {
let newStarred = Favourite(snapshot: child as! FIRDataSnapshot)
self.detailStarred = newStarred
//print(newStarred)
var boolStorage: Bool? = self.detailStarred?.favourite
if let boolStorage = boolStorage, boolStorage {
// executes when booleanValue is true
let myImage = UIImage(named: "filled 64x64.png")
self.starButton.setImage(myImage, for: UIControlState.normal)
self.buttonStarred = "fav"
print(self.buttonStarred)
}
else {
//self.detailStarred?.favourite = true
print("not a favourite")
self.buttonStarred = "notFav"
print(self.buttonStarred as Any)
}
}}})
}

然后我的按钮会进行额外的检查:

if self.buttonStarred == "missing" {   //extra check to avoid false to be the value for first time creation in FireBase
let favourite = ["favourite": true, "realIndex": detailWhisky?.realIndex as Any, "shortName": self.title as Any, "distillery": distilleryLabel.text as Any, "searchText": searchText as Any] as [String : Any] //21.09.0217
starRef2.updateChildValues(favourite, withCompletionBlock: { (err, ref) in
if err != nil {
print(err as Any)
return
} })}

现在可以了。它会立即填充按钮,因此对于用户来说是无缝的,并将正确的状态写入 firebase。

下面的完整按钮代码,以防它可以帮助某人:

@IBAction func starButton(_ sender: Any) {
//loads info for query
let currentUserID = self.user?.uid
let realIndex:Int? = detailWhisky?.realIndex
let realIndex2 = "\(realIndex!)"
let searchText: String? = ("\(self.distilleryLabel.text)"+"\(self.title)"+"\(self.brandNameLabel.text)")//creates searchText
let filledImage = UIImage(named: "filled 64x64.png") //21.09.0217
//executes query
let starRef2 = FIRDatabase.database().reference().child("Starred").child(currentUserID!).child(realIndex2)
//print("booyah")
self.starButton.setImage(filledImage, for: UIControlState.normal)//21.09.0217
let favourite = ["favourite": true, "realIndex": detailWhisky?.realIndex as Any, "shortName": self.title as Any, "distillery": distilleryLabel.text as Any, "searchText": searchText as Any] as [String : Any] //21.09.0217
starRef2.updateChildValues(favourite, withCompletionBlock: { (err, ref) in
if err != nil {
print(err as Any)
return
}})
//changes state of button and loads true/false to Firebase
isStarred = !isStarred

if self.buttonStarred == "missing" { //extra check to avoid false to be the value for first time creation in FireBase
let favourite = ["favourite": true, "realIndex": detailWhisky?.realIndex as Any, "shortName": self.title as Any, "distillery": distilleryLabel.text as Any, "searchText": searchText as Any] as [String : Any] //21.09.0217
starRef2.updateChildValues(favourite, withCompletionBlock: { (err, ref) in
if err != nil {
print(err as Any)
return
} })}
else if isStarred == false {
let image = UIImage(named: "star64x64.png")
(sender as AnyObject).setImage(image, for: UIControlState.normal)
let favourite = ["favourite": false, "realIndex": detailWhisky?.realIndex as Any, "shortName": self.title as Any, "distillery": distilleryLabel.text as Any, "searchText": searchText as Any] as [String : Any]
starRef2.updateChildValues(favourite, withCompletionBlock: { (err, ref) in
if err != nil {
print(err as Any)
return
} })}
else {
let image = UIImage(named: "filled 64x64.png")
(sender as AnyObject).setImage(image, for: UIControlState.highlighted)
let favourite = ["favourite": true, "realIndex": detailWhisky?.realIndex as Any, "shortName": self.title as Any, "distillery": distilleryLabel.text as Any, "searchText": searchText as Any] as [String : Any]
starRef2.updateChildValues(favourite, withCompletionBlock: { (err, ref) in
if err != nil {
print(err as Any)
return
} })}
//isStarred = !isStarred
UserDefaults.standard.set(isStarred, forKey: "isStarred")
UserDefaults.standard.synchronize()
}

关于ios - 第一次按按钮不起作用,但第二次就可以了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43075369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com