如何通过UITabBarController将indexPathForSelectedRow传递到UITabView?

时间:2018-07-31 04:10:44

标签: ios swift uitableview uitabbarcontroller

我正在尝试从UITableViewUITabBarController中的特定选项卡进行筛选。在进行谷歌搜索时,我发现两组信息似乎可以指示操作方法,但都没有使用UITableView作为源。

第一个来源是我在StackOverflow上找到的这个奇妙的答案:How to make a segue to second item of tab bar?

第二个来源是此站点:http://www.codingexplorer.com/segue-uitableviewcell-taps-swift/

我一直在尝试将两者结合用于我的应用程序。以下是我的原始UIViewController的截短版本(我可以在需要时发布完整的版本,只是我认为大多数代码与该命令无关):

class BonusListViewController: UITableViewController {

    // MARK: - Table View Configuration
        override func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }

        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if isFiltering() {
                print("Showing \(filteredBonuses.count) Filtered Results")
                return filteredBonuses.count
            }

            print("Found \(bonuses.count) rows in section.")
            return bonuses.count
        }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRow(at: indexPath as IndexPath, animated: true)

        let row = indexPath.row
    }

    private var nextViewNumber = Int()

    @IBAction func secondView(_ sender: UITapGestureRecognizer) {
        self.nextViewNumber = 2
        self.performSegue(withIdentifier: "tabBar", sender: self)
    }   

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "tabBar" {

                let destination = segue.destination as! TabBarViewController

                switch (nextViewNumber) {
                case 1:
                    destination.selectedIndex = 0

                case 2:
                    destination.selectedIndex = 1
                    if self.isFiltering() {
                        destination.bonus = filteredBonuses[(tableView.indexPathForSelectedRow?.row)!]
                    } else {
                        destination.bonus = bonuses[(tableView.indexPathForSelectedRow?.row)!]
                    }

                default:
                    break
                }
            }
      }
}

我的问题解决了尝试将tableView.indexPathForSelectedRow?.row传递到UITabViewController的问题。在上述代码段末尾的prepare(for segue)中,destination.bonus =行的编译错误是

  

'TabBarViewController'类型的值没有成员'bonus'

从技术上讲,这是正确的,因为我只是试图将TabBarViewController传递到它控制的第二个选项卡。

如何解决以上问题,让我点击一个单元格,然后将所选行传递到目标UITabView

编辑:如果有帮助,这里是情节提要的图片。 App Storyboard

1 个答案:

答案 0 :(得分:1)

  

'TabBarViewController'类型的值没有成员'bonus'   因为“ TabBarViewController”没有名为Bonus的属性

您可以继承TabBarViewController并添加属性bonus

并通过

进行设置
guard let destination = segue.destination as? YourTabbarSubClass else {return }

您可以通过bonus来访问destination.bonus

现在,当您需要标签栏控制器中的bonus时,可以将其与(self.tabbarController as! YourTabbarSubClass).bonus一起使用

编辑

class  TabBarViewController: UITabBarController {
   // Add property bonus here
     var bouns:BounsStruct?

}

现在从您的视图控制器中获取所需的

class YourFirstTabVC:UIVIewController {
   //where you need that 
   self.bouns = (self.tabbarController as!  TabBarViewController).bouns
   self.tableview.reloadData()


}