我想将用户名设置为标签,因为我的 block ("findObjectsInBackgroundWithBlock") 需要时间才能完成并且 label.text 设置了 nil 。
完成区 block 后如何设置标签文字??
class test {
var userName: String? // variable to access user name returned by block
private func loadData() {
getCurrentUser()
lblUserName.text = userName
}
}
这是我从 Parse 获取用户数据的 block 。
private func getCurrentUser() {
if PFUser.currentUser() != nil {
currentUser = PFUser.currentUser()?.username
let query = PFQuery(className: "_User")
query.whereKey("username", equalTo: currentUser!)
query.findObjectsInBackgroundWithBlock { (currentUsers, error) -> Void in
if error == nil {
for user in currentUsers! {
userName = user["name"] as? String
}
}
}
}
}
最佳答案
你可以像这样使用属性观察器:
var userName: String? {
didset {
if let name = username {
lblUserName.text = name
}
}
}
关于ios - 完成 block Swift 后设置标签文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35308869/