检索firebase信息以设置按钮标题



我正试图从firebase中检索信息,以设置按钮标题,从而允许用户以不同的方式与视图交互。我不知道该怎么做,所以我会发布一些代码,希望有人能理解。

func fetchJobProgress() {
let workerId = workerUser?.uid
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("workInProgress").child(uid!).queryOrdered(byChild: "fromId").queryEqual(toValue: workerId).observeSingleEvent(of: .value, with: { (snapshot) in

self.confirmButton.backgroundColor = GREEN_Theme
self.confirmButton.addTarget(self, action: #selector(self.handleConfirm), for: .touchUpInside)



}, withCancel: { (err) in
print("attempting to load information")
})
}

将函数更改为:

func fetchJobProgress() {
let workerId = workerUser?.uid
let uid = Auth.auth().currentUser?.uid
Database.database().reference().child("workInProgress").child(uid!).queryOrdered(byChild: "fromId").queryEqual(toValue: workerId).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String : Any] else { return }
for (_, valueDic) in dictionary {
guard let dic = valueDic as? [String: Any] else {
continue
}
guard let type = dic["type"] as? Int else {
continue
}
if type == userNotifications.NotificationType.accepted.rawValue {
self.confirmButton.backgroundColor = GREEN_Theme
self.confirmButton.addTarget(self, action: #selector(self.handleConfirm), for: .touchUpInside)
} else if type == userNotifications.NotificationType.inProgress.rawValue {
self.confirmButton.setTitle("In Progress...", for: .normal)
self.confirmButton.backgroundColor = UIColor.lightGray
self.confirmButton.addTarget(self, action: #selector(self.handleComplete), for: .touchUpInside)
} else if type == userNotifications.NotificationType.confirmCompleted.rawValue {
self.confirmButton.setTitle("Completed!", for: .normal)
self.confirmButton.backgroundColor = GREEN_Theme
self.confirmButton.isUserInteractionEnabled = false

}
break
}


}, withCancel: { (err) in
print("attempting to load information")
})
}

最新更新