如何在文本字段中显示学校 ID?
How to display school ID in the textfield?
我想在某个视图控制器的文本字段中显示学校 ID,但由于某些原因我不能。我有一个函数可以获取当前 School 用户的学校 ID,我可以在警报 VC 中显示它,但不能在常规 VC 中显示它。我会添加图片和解释。
首先,我将展示获取当前用户的学校 ID 并将其显示在警报中的函数的代码VC。
func getTheSchoolsID() -> String {
db.collection("school_users").whereField("userID", isEqualTo: user?.uid).getDocuments { (querySnapshot, err) in
if let err = err {
print("There was an error fetching the documents: \(err)")
} else {
self.skoolsID = querySnapshot!.documents.map { document in
return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
}
self.tableView.reloadData()
}
}
let fixID = "\(skoolsID)"
let substring = fixID.dropFirst(28).dropLast(3)
let realString = String(substring)
return realString
}
这里也是警报的代码VC。
@IBAction func infoPressed(_ sender: UIBarButtonItem) {
var text = UITextField()
let alert = UIAlertController(title: "User Info", message: "School ID", preferredStyle: .alert)
let action = UIAlertAction(title: "Dismiss", style: .cancel) { (action) in
self.dismiss(animated: true, completion: nil)
}
alert.addTextField { (alertTextField) in
alertTextField.text = self.getTheSchoolsID()
alertTextField.isEnabled = false
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
这两个产生了这个结果。
这也是文档中的 school_id。
现在,当我想在另一个 VC 添加新事件的文本字段中显示完全相同的值时,它什么也没显示。
代码与第一个代码片段完全相同,除了 reloadData() 是有道理的,而且它不会产生相同的结果。
func getTheSchoolID() -> String {
db.collection("school_users").whereField("userID", isEqualTo: user?.uid).getDocuments { (querySnapshot, err) in
if let err = err {
print("There was an error fetching the documents: \(err)")
} else {
self.skoolID = querySnapshot!.documents.map { document in
return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
}
}
}
let fixedID = "\(skoolID)"
let substring = fixedID.dropFirst(28).dropLast(3)
let realString = String(substring)
return realString
}
这也是我试图声明的。
public override func viewDidLoad() {
super.viewDidLoad()
errLabel.alpha = 0
getTheSchoolID()
schoolIDTextF.text = getTheSchoolID()
// Do any additional setup after loading the view.
}
如果有人能帮助解决这个问题,我们将不胜感激。我尽力解释它。我知道有很多信息,但如果您还需要更多信息,我可以在评论中澄清。谢谢。
问题出在异步 Firebase 代码上。使用闭包或完成处理程序来获取结果。
修改你的getSchoolId为
func getTheSchoolID(completion: @escaping ((String?) -> ())) {
db.collection("school_users").whereField("userID", isEqualTo: user?.uid).getDocuments { (querySnapshot, err) in
if let err = err {
print("There was an error fetching the documents: \(err)")
completion(nil)
} else {
self.skoolID = querySnapshot!.documents.map { document in
return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
}
}
}
let fixedID = "\(skoolID)"
let substring = fixedID.dropFirst(28).dropLast(3)
let realString = String(substring)
completion(realString)
}
并在您的 VC 中用作
override func viewDidLoad() {
super.viewDidLoad()
errLabel.alpha = 0
getTheSchoolID(completion: {
[weak self] schoolId in
if let id = schooldId {
DispatchQueue.main.async {
self?.schoolIDTextF.text = id
}
}
})
}
firestore 集合中的 GetSchoolIds 是异步任务。所以使用闭包你可以实现这个。
func getTheSchoolsID(completion:@escaping(_ schoolIds:[SchoolID]?) -> Void) {
db.collection("school_users").whereField("userID", isEqualTo: user?.uid).getDocuments { (querySnapshot, err) in
if let err = err {
print("There was an error fetching the documents: \(err)")
completion(nil)
} else {
let schoolsId = querySnapshot!.documents.map { document in
return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
}
completion(schoolsId)
}
}
}
将此功能用于您的VC
override func viewDidLoad() {
super.viewDidLoad()
errLabel.alpha = 0
getTheSchoolsID(completion: {
[weak self] schoolIds in
if let schoolIdObj = schoolIds?.first {
let substring = schoolIdObj.schoolID.dropFirst(28).dropLast(3)
let realString = String(substring)
DispatchQueue.main.async {
self?.schoolIDTextF.text = realString
}
}
})
}
我想在某个视图控制器的文本字段中显示学校 ID,但由于某些原因我不能。我有一个函数可以获取当前 School 用户的学校 ID,我可以在警报 VC 中显示它,但不能在常规 VC 中显示它。我会添加图片和解释。
首先,我将展示获取当前用户的学校 ID 并将其显示在警报中的函数的代码VC。
func getTheSchoolsID() -> String {
db.collection("school_users").whereField("userID", isEqualTo: user?.uid).getDocuments { (querySnapshot, err) in
if let err = err {
print("There was an error fetching the documents: \(err)")
} else {
self.skoolsID = querySnapshot!.documents.map { document in
return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
}
self.tableView.reloadData()
}
}
let fixID = "\(skoolsID)"
let substring = fixID.dropFirst(28).dropLast(3)
let realString = String(substring)
return realString
}
这里也是警报的代码VC。
@IBAction func infoPressed(_ sender: UIBarButtonItem) {
var text = UITextField()
let alert = UIAlertController(title: "User Info", message: "School ID", preferredStyle: .alert)
let action = UIAlertAction(title: "Dismiss", style: .cancel) { (action) in
self.dismiss(animated: true, completion: nil)
}
alert.addTextField { (alertTextField) in
alertTextField.text = self.getTheSchoolsID()
alertTextField.isEnabled = false
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
这两个产生了这个结果。
这也是文档中的 school_id。
现在,当我想在另一个 VC 添加新事件的文本字段中显示完全相同的值时,它什么也没显示。
代码与第一个代码片段完全相同,除了 reloadData() 是有道理的,而且它不会产生相同的结果。
func getTheSchoolID() -> String {
db.collection("school_users").whereField("userID", isEqualTo: user?.uid).getDocuments { (querySnapshot, err) in
if let err = err {
print("There was an error fetching the documents: \(err)")
} else {
self.skoolID = querySnapshot!.documents.map { document in
return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
}
}
}
let fixedID = "\(skoolID)"
let substring = fixedID.dropFirst(28).dropLast(3)
let realString = String(substring)
return realString
}
这也是我试图声明的。
public override func viewDidLoad() {
super.viewDidLoad()
errLabel.alpha = 0
getTheSchoolID()
schoolIDTextF.text = getTheSchoolID()
// Do any additional setup after loading the view.
}
如果有人能帮助解决这个问题,我们将不胜感激。我尽力解释它。我知道有很多信息,但如果您还需要更多信息,我可以在评论中澄清。谢谢。
问题出在异步 Firebase 代码上。使用闭包或完成处理程序来获取结果。
修改你的getSchoolId为
func getTheSchoolID(completion: @escaping ((String?) -> ())) {
db.collection("school_users").whereField("userID", isEqualTo: user?.uid).getDocuments { (querySnapshot, err) in
if let err = err {
print("There was an error fetching the documents: \(err)")
completion(nil)
} else {
self.skoolID = querySnapshot!.documents.map { document in
return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
}
}
}
let fixedID = "\(skoolID)"
let substring = fixedID.dropFirst(28).dropLast(3)
let realString = String(substring)
completion(realString)
}
并在您的 VC 中用作
override func viewDidLoad() {
super.viewDidLoad()
errLabel.alpha = 0
getTheSchoolID(completion: {
[weak self] schoolId in
if let id = schooldId {
DispatchQueue.main.async {
self?.schoolIDTextF.text = id
}
}
})
}
firestore 集合中的 GetSchoolIds 是异步任务。所以使用闭包你可以实现这个。
func getTheSchoolsID(completion:@escaping(_ schoolIds:[SchoolID]?) -> Void) {
db.collection("school_users").whereField("userID", isEqualTo: user?.uid).getDocuments { (querySnapshot, err) in
if let err = err {
print("There was an error fetching the documents: \(err)")
completion(nil)
} else {
let schoolsId = querySnapshot!.documents.map { document in
return SchoolID(schoolID: (document.get("school_id") as! String ?? ""))
}
completion(schoolsId)
}
}
}
将此功能用于您的VC
override func viewDidLoad() {
super.viewDidLoad()
errLabel.alpha = 0
getTheSchoolsID(completion: {
[weak self] schoolIds in
if let schoolIdObj = schoolIds?.first {
let substring = schoolIdObj.schoolID.dropFirst(28).dropLast(3)
let realString = String(substring)
DispatchQueue.main.async {
self?.schoolIDTextF.text = realString
}
}
})
}