gpt4 book ai didi

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

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

我正在尝试从 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 无关,我不认为):

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 = 行的编译错误,

Value of type 'TabBarViewController' has no member 'bonus'

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

如何解决上述问题,让我点击一个单元格,然后将选定的行传递给目标 UITabView

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

最佳答案

Value of type 'TabBarViewController' has no member 'bonus' Because 'TabBarViewController' has no property named bonus

您可以继承 TabBarViewController 添加属性 bonus

然后像 segue 一样设置它

guard let destination = segue.destination as? YourTabbarSubClass else {return }

并且您可以通过 destination.bonus 访问 bonus

现在,当您需要标签栏 Controller 的 bonus 时,您可以将其与 (self.tabbarController as! YourTabbarSubClass).bonus

一起使用

编辑

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

}

现在从你需要的 View Controller

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


}

关于ios - 如何通过 UITabBarController 将 indexPathForSelectedRow 传递到 UITabView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51605333/

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