2 uitabbarcontroller uitableview ios swift
我想将图像和新闻从视图控制器传递到连接的选项卡栏控制器的第一个选项卡。NewsViewController 的第一个选项卡不显示新闻标题和图像。didSelectRowAt indexPath 方法如下。
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=sneakersnews[indexPath.row].image
DVC.getNews=sneakersnews[indexPath.row].news
self.navigationController?.pushViewController(tabBar, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
如何从表视图中传递并显示第一个选项卡 NewsViewController 上的新闻和图像?
您可以从此链接下载该项目:
https://drive.google.com/file/d/1qfI3UaXoUTzOS6Jfe40wj99NAifVkz5Y/view?usp=sharing **
首先你在做两件不同的事情
DiscoveryNewsViewController 和 tabbarController 之间有一个 segue
你正在像这样推动标签栏控制器
let DVC = tabBar.viewControllers?[0] as! NewsViewController
//this is not required as it will always open UIViewController at Zero index so comment this
//tabBar.selectedIndex = 0
let image = sneakersnews[indexPath.row].image
DVC.getImage = image
let news = sneakersnews[indexPath.row].news
DVC.getNews = news
self.navigationController?.pushViewController(tabBar, animated: true)
Run Code Online (Sandbox Code Playgroud)请阅读上面代码中的注释行
因此,您不能同时执行上述两点,因为 segue 连接已经在推送您的 TabBarController 并且您的 self.navigationController?.pushViewController 也在推送 TabBarController。
解决方案:-
2.删除DiscoveryNewsViewController和tabbarController之间的segue连接
| 归档时间: |
|
| 查看次数: |
1078 次 |
| 最近记录: |