按钮到表视图控制器并执行过滤器功能

时间:2019-02-22 14:41:42

标签: ios xcode

请原谅我是一名新程序员,不熟悉编码 所以我的问题是我想在Viewcontroller1中创建一个按钮,该按钮触发要转到另一个视图控制器的操作(我做到了),并同时调用TutorListTableViewController的功能(过滤表视图)(在另一个类中)

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

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()
    })

}

2 个答案:

答案 0 :(得分:0)

我认为可能的解决方法是

在TutorListViewController的viewDidLoad方法中,如果索引具有正确的值,则调用observeEnglishCases:

例如

SELECT
    *
FROM
    table
WHERE
    regions = 'Europe' or
    regions like 'Europe,%' or
    regions like '%,Europe' or
    regions like '%,Europe,%' or
    regions = 'Middle East' or
    regions like 'Middle East,%' or
    regions like '%,Middle East' or
    regions like '%,Middle East,%'

答案 1 :(得分:0)

正如已经指出的那样,您需要阅读如何传递信息。 (代理,通知,阻止),以供您日后参考:

  • ViewController1可以有一位代表来告诉某人“ englishGO”已被窃听
  • 有人必须引用TutorListViewController实例,并且在检测到“ englishGO”点击时,它将在该实例上调用observeEnglishCases()

但是,在您当前的实现中,这可能会起作用:

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

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