首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过indexPathForSelectedRow将UITabView传递给UITabBarController?

如何通过indexPathForSelectedRow将UITabView传递给UITabBarController?
EN

Stack Overflow用户
提问于 2018-07-31 04:10:43
回答 1查看 140关注 0票数 1

我正在尝试从一个UITableView转换到一个UITabBarController中的特定选项卡。在谷歌搜索时,我发现了两组信息,它们似乎表明了该如何做,但两者都没有使用UITableView作为源。

第一个来源是我在StackOverflow:How to make a segue to second item of tab bar?上找到的这个精彩的书面答复。

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

我一直试图在我的应用程序中将两者结合起来。下面是我的原始UIViewController的截断版本(如果需要的话,我可以发布完整的版本,只是大部分代码与这个segue无关,我不认为):

代码语言:javascript
复制
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”类型的值没有成员“奖励”

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

如何修正上面的内容,让我点击一个单元格,然后将选定的行传递给目标UITabView

编辑:如果有帮助,这里有一张故事板的图片。

EN

回答 1

Stack Overflow用户

发布于 2018-07-31 04:54:40

类型“TabBarViewController”的值没有成员“奖励”,因为“TabBarViewController”没有名为“奖励”的属性。

您可以子类TabBarViewController添加属性bonus

并将其设置为

代码语言:javascript
复制
guard let destination = segue.destination as? YourTabbarSubClass else {return }

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

现在,当您需要来自选项卡控制器的bonus时,您可以将它与(self.tabbarController as! YourTabbarSubClass).bonus一起使用。

编辑

代码语言:javascript
复制
class  TabBarViewController: UITabBarController {
   // Add property bonus here
     var bouns:BounsStruct?

}

现在,从您的视图控制器,您需要的地方

代码语言:javascript
复制
class YourFirstTabVC:UIVIewController {
   //where you need that 
   self.bouns = (self.tabbarController as!  TabBarViewController).bouns
   self.tableview.reloadData()


}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51605333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档