我对整体编码很陌生,学习swift 3,而且我正在尝试整合按下“星星”的按钮。因此,目前它写入Firebase,状态为“true”,如果按下则会显示更多数据。
问题是它只能按下2次。但之后它完美地工作...... (编辑:“不工作”我的意思是我第一次按下它时不会将图像更新为“填充64x64”。我刚刚在Firebase中检查过,它似乎创建了一个记录,我第一次按,但是把它当作最爱=假)
一些经验丰富的编程员可以帮我理解什么是错的吗?
这是“星”按钮的代码:
*scanf
然后填充我在ViewDidLoad中运行“starred()”。
/*
==========================================================================================
//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()
}
“starred()”函数如下所示:
override func viewDidLoad() {
super.viewDidLoad()
configureView() //populates text fields etc.
starred() // populates the starred star
}
Data struct看起来像这样:
//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")}
}}})
}
}
答案 0 :(得分:0)
我找到了一个适合我的解决方案。我相信它可以更优雅地处理,但我发布它以防它可以帮助别人。
解决方案是添加一个额外的变量" buttonStarred"如果以前不受欢迎,它会从Firebase获得一个值。然后,当"已加星标的按钮"在我继续使用现有代码之前按下了。
func从firebase加载收藏夹的状态。现在它给出的值是"缺少" to" buttonStarred"变量
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()
}