【问题标题】:How to pass data from view controller to first tab of tab bar controller如何将数据从视图控制器传递到选项卡栏控制器的第一个选项卡
【发布时间】:2018-06-08 16:18:35
【问题描述】:

我想将图像和新闻从视图控制器传递到连接的选项卡控制器的第一个选项卡。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)
}

如何在表格视图的第一个标签 NewsViewController 上传递和显示新闻和图像?

您可以从此链接下载项目:

https://drive.google.com/file/d/1qfI3UaXoUTzOS6Jfe40wj99NAifVkz5Y/view?usp=sharing**

【问题讨论】:

  • 您面临的问题是什么?
  • 标签栏控制器的第一个标签(NewsViewController)没有在视图控制器上显示图像和新闻标题。
  • 尝试使用 Segue。
  • 项目中的标签栏为零
  • 在 viewDidLoad 中添加一个检查,它将解决您的问题。

标签: ios swift uitableview uitabbarcontroller


【解决方案1】:

首先你在做两件不同的事情

  1. DiscoveryNewsViewController 和 tabbarController 之间存在segue

  2. 您正在像这样推送标签栏控制器

    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)
    

请阅读上面代码中的注释行

因此,您不能同时执行上述两点,因为 segue 连接已经在推送您的 TabBarController 并且您的 self.navigationController?.pushViewController 也在推送 TabBarController。

解决方案:-

  1. 只需将 DiscoveryNewsViewController 嵌入到导航控制器中

2.移除DiscoveryNewsViewController和tabbarController之间的segue连接

【讨论】:

  • 我在侧标签栏控制器中有表格视图控制器。问题是当我在表格视图中选择一行时单击它不会从现有标签栏控制器加载第二个标签栏控制器......您可以从此链接下载此代码。drive.google.com/file/d/10w9MQaRxGlMSG_lK07N1c7nJWQLC8BWc/…
  • @ajadka 我想我已经回答了您的问题。当您接受任何人的回答然后将其标记为未接受时,这是不好的,因为现在您的要求已更改。您应该将其作为另一个问题提出。跨度>
【解决方案2】:
  1. 您不应该将标签栏推入导航控制器。因此,删除 DiscoveryNewsViewController 和 TabBar 之间的故事板中的 segue

所以替换

self.navigationController?.pushViewController(tabBar, animated: true)

self.present(tabBar, animated: true, completion: nil)
  1. 由于您没有在 UI 中刷新为图像和标签分配的值,因此它不会显示。为此,只需创建一个函数

    函数刷新数据() { newsTitle.text = getNews 新闻图片.image = 获取图片 }

在 NewsViewController 中 并在

之后调用它
DVC.getImage=sneakersnews[indexPath.row].image
DVC.getNews=sneakersnews[indexPath.row].news

喜欢这个

DVC.refreshData()

【讨论】:

  • @navneet 为什么他不能推?
  • 我没有在我的代码 self.navigationController?.pushViewController(tabBar, animated: true) 中使用此语句......相反,我在 didSelectRowAt indexPath 中使用这两个语句...... let storybaord=UIStoryboard(name: "Main", bundle: nil) let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
  • @user511 因为不是苹果自己推荐的……可能会引起奇怪的行为
  • 基本上你是通过 segue 推送 tabbar 并且还在 didselectrow 方法中创建一个新的 tabbar 实例......你需要修复你想要使用的那个
【解决方案3】:

我看到了你的代码,并得到了问题和许多测试!

不是一个复杂的问题,但下次需要注意。

这是因为你将一个segue从你的cell连接到一个TabbarController,当你tab cell时,它会因为segue而自动转到tabbarController。

然后你初始化另一个 tabbarController 并传递数据然后呈现它。您在 NewsViewController viewDidLoad 中设置一个断点,可以看到两次命中。

您的代码中还有许多其他问题, 在我看来,你只能使用 segue 来传递数据,以使你的代码稳定且可预测

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    相关资源
    最近更新 更多