我有一个带tabbarcontroller的故事板。标签栏之一有一个tableview,我希望当用户从tableview中点击一行时,用另一个标签栏打开一个详细视图。 问题是当我打开详细视图标签栏隐藏时。如何在didselectrowatindexpath显示tabbar?
didselectrowatindexpath中的代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storybaord=UIStoryboard(name: "Main", bundle: nil)
let DVC=storybaord.instantiateViewController(withIdentifier: "NewsViewController") as! NewsViewController
DVC.getImage=sneaker[indexPath.row]
DVC.getNews=News[indexPath.row]
self.navigationController?.pushViewController(DVC, animated: true)
}
我创建了单视图应用程序。我创建了DiscoveryNewsTableViewCell.I用两个数组填充了表视图单元格1-sneaker 2-新闻数组。我希望当特定数组显示bar控制器的ist选项卡时。但我确实得到了这些标签栏控制器第一个标签上的字段。这是此https://drive.google.com/file/d/1uSYl-4a1UQXpMXkSQ_l8aij6EH_v2yna/view?usp=sharing
的示例代码答案 0 :(得分:1)
尝试以这种方式修改代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// setting these values in local storage using user defaults
let currentObj = News[indexPath.row]
UserDefaults.standard.set(currentObj, forKey: "currentOb")
let storybaord=UIStoryboard(name: "Main", bundle: nil)
let DVC=storybaord.instantiateViewController(withIdentifier: "NewsViewController") as! NewsViewController
self.navigationController?.pushViewController(DVC, animated: true)
}
答案 1 :(得分:0)
这是因为你正在推动viewController而不是TabBarController。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storybaord=UIStoryboard(name: "Main", bundle: nil)
let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
let DVC = tabBar.viewControllers[0] as! NewsViewController
tabBar.selectedIndex = 0
DVC.getImage=sneaker[indexPath.row]
DVC.getNews=News[indexPath.row]
self.navigationController?.pushViewController(tabBar, animated: true)
}
在上面的代码中替换
中的0 tabBar.viewControllers[0]
tabBar.selectedIndex = 0
带有要打开的视图控制器的索引。
还在行
let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
必须将tabBar 设置为要显示的UITabBarController的故事板标识符,如下所示。