gpt4 book ai didi

ios - TableView Controller 的按钮并执行过滤功能

转载 作者:行者123 更新时间:2023-11-29 05:59:28 26 4
gpt4 key购买 nike

请原谅我是一个新程序员,对编码不熟悉所以我的问题是我想在 Viewcontroller1 中创建一个按钮,它触发操作转到另一个 View Controller (我做到了),并同时调用 TutorListTableViewController (在另一个类中)的函数(过滤 TableView )

我不确定调用另一个类的函数是否是正确的方法。如果是的话,我仍然无法成功调用该函数。任何帮助深表感谢!错误显示如下:

ViewController1 的按钮操作

@IBAction func englishGO(_ sender: Any) {
self.tabBarController?.selectedIndex = 1
TutorListViewController.observeEnglishCases()
}

TutorListTableViewController的功能

func observeEnglishCases(){
let casesRef = Database.database().reference().child("cases")
let queryRef = casesRef.child("school").queryEqual(toValue: "Ling")
queryRef.observe(.value, with: { snapshot in

var tempPosts = [Case]()


for child in snapshot.children {
if let childSnapshot = child as? DataSnapshot,
let dict = childSnapshot.value as? [String:Any],
let name = dict["name"] as? String,
let gender = dict["gender"] as? String,
let school = dict["school"] as? String,
let year = dict["year"] as? String,
let major = dict["major"] as? String,
let tutorsubject = dict["tutorsubject"] as? String,
let tutoryear = dict["tutoryear"] as? String,
let tutorperformance = dict["tutorperformance"] as? String,
let region = dict["region"] as? String,
let minimumprice = dict["minimumprice"] as? String,
let thingtospeak = dict["thingtospeak"] as? String,
let photoURL = dict["photoURL"] as? String,
let url = URL(string:photoURL){
let caser = Case(name: name, gender: gender, school: school, year: year, major: major, tutorsubject: tutorsubject, tutoryear: tutoryear, tutorperformance: tutorperformance, region: region, minimumprice: minimumprice, thingtospeak: thingtospeak, photoURL: url)
tempPosts.insert(caser, at:0)
}
}
self.cases = tempPosts
self.tutorCasesCollectionView.reloadData()
})

}

最佳答案

正如已经指出的,您需要阅读如何传递信息。 (委托(delegate)、通知、 block )为您提供稍后的总体思路:

  • ViewController1 可以有一个委托(delegate)来告诉某人“englishGO”已被点击
  • 某人必须引用 TutorListViewController 实例,并且当检测到“englishGO”点击时,它会在该实例上调用 observeEnglishCases()

但是,在您当前的实现中,这可能有效:

@IBAction func englishGO(_ sender: Any) {
self.tabBarController?.selectedIndex = 1
(self.tabBarController.viewControllers[1] as? TutorListViewController)?. observeEnglishCases()
}

observeEnglishCases() 是一个实例方法,必须这样调用。通过编写 TutorListViewController.observeEnglishCases(),您调用了一个类方法,该方法可能在您的代码中不存在。

关于ios - TableView Controller 的按钮并执行过滤功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54829506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com