如果单身人士的价值改变,更新标签 [英] Updating label if value in singleton changes
问题描述
我应该如何更新标签如果在单身人士的价值变化?
我有选项卡视图。在第一个选项卡中,我分配了值,在第二个选项卡中,我尝试着将这些值加上标签。
这是我的单身人士:
class CurrentUser:NSObject
{
var generalDetails:User = User()///一行中的连续声明必须用';'
static let sharedInstance = CurrentUser()
fileprivate override init(){
super.init()
}
$ b $ p
$ b 像这样我赋值:
$
$ b pre $ self $ db $ self.databaseRef.child(users)。child(user.uid).observeSingleEvent(of:.value){(snapshot:FIRDataSnapshot) b
后卫let firebaseValue = snapshot.value as? [String:AnyObject],让userName = firebaseValue [username] as?字符串,让电子邮件= firebaseValue [电子邮件]为?字符串,让声誉= firebaseValue [信誉]为?诠释,让profilePicURL = firebaseValue [profileImageUrl]作为? String
else
{
print(FrB快照错误)//这里
返回
}
//设置值
self.currentUser.generalDetails = User(userName:userName,email:email,reputation:reputation,profileImageURL:profilePicURL,uid:user.uid)
}
如果我想把值放到标签上,我只需要做这个(这个声望是唯一可以经常改变的):
b$ b
self.reputationLabel.text = String(self.currentUser.generalDetails.reputation)
您可以执行以下任一操作: - $ / $>
$ b $ ul
使用委托协议方法在单例和您的类之间进行通信,只要您的回购协议发生变化并更新您的标签,就在类中激发委托方法。
在你的网络链接中打开一个不同的线程用户在viewController本身的声望: -
override func viewDidLoad(){
super.viewDidLoad()$ b (.childChanged,.child)。
$ b FIRDatabase.database()。reference()。child(users)。child(FIRAuth.auth()!. currentUser!.uid).child与:{(快照)在
print(Snapshot.value!)
//更新你的标签
})
每当信誉值发生变化时都会被调用。
I am getting some user information from Firebase and storing it into singleton. After that every time the value changes I want that the label changes also but it doesn't until I terminate the app and come back in.
How should I update label if value changes in singleton?
I have tab views. In first tab I assign values and in second tab I try to put the values to label.
This is my singleton:
class CurrentUser: NSObject
{
var generalDetails: User = User()/// Consecutive declarations on a line must be separated by ';'
static let sharedInstance = CurrentUser()
fileprivate override init(){
super.init()
}
}
And like this I assign values:
self.databaseRef.child("users").child(user.uid).observeSingleEvent(of: .value) { (snapshot:FIRDataSnapshot) in
guard let firebaseValue = snapshot.value as? [String:AnyObject], let userName = firebaseValue["username"] as? String, let email = firebaseValue["email"] as? String, let reputation = firebaseValue["reputation"] as? Int, let profilePicURL = firebaseValue["profileImageUrl"] as? String
else
{
print("Error with FrB snapshot")//Here
return
}
//Set values
self.currentUser.generalDetails = User(userName: userName, email: email, reputation: reputation, profileImageURL: profilePicURL, uid: user.uid)
}
And if I want to put the value to the label I simply do this(This reputation is the only thing that can change often):
self.reputationLabel.text = String(self.currentUser.generalDetails.reputation)
You can do either of these:-
Communicate between the singleton and your class with delegate-protocol method , fire the delegate method in the class whenever your repo changes and update your label.
Open a different thread in your network link for the user's reputation in the viewController itself:-
override func viewDidLoad() { super.viewDidLoad() FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid).child("reputation").observe(.childChanged, with: {(Snapshot) in print(Snapshot.value!) //Update yor label })
which will get called every time the value of reputation changes.
这篇关于如果单身人士的价值改变,更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!