【问题标题】:How to pass value from one tabbar View item to other? [duplicate]如何将值从一个标签栏查看项目传递给另一个? [复制]
【发布时间】:2019-08-15 14:21:56
【问题描述】:

我在新闻标签中有一个按钮,我想以任何方式将价值传递给竞赛标签。

但问题在于没有传递正确的值。

我的代码是-

//NewsViewController

@IBAction func fullStndngAction(_ sender: Any) {
        //I Tried this
        let prefs = UserDefaults.standard
        prefs.set(true, forKey: "isFromNewsView")

        //I Tried This as well
        let whereFrom  = storyboard?.instantiateViewController(withIdentifier: "LatestNewsDetailViewController") as? LatestNewsDetailViewController
        whereFrom!.isFromNewsView = true

        self.tabBarController?.selectedIndex = 1

    }

但我总是在竞争观点上变得虚假。

图片说明-

【问题讨论】:

    标签: swift uitabbar


    【解决方案1】:

    根据每种情况尝试以下解决方案之一。

    案例 1:如果您的 ViewController 嵌入在 navigationController 中,请尝试以下操作:-

    if let competitionsVC = self.tabBarController.viewControllers?[1].children.first as? CompetitionViewController {
     competitionVC.isFromNewsView = true
     self.tabBarController?.selectedIndex = 1
    }
    

    案例 2:如果你的 ViewController 没有嵌入到 navigationController 中

    if let competitionVC = self.tabBarController.viewControllers?[1] as? CompetitionViewController {
     competitionVC.isFromNewsView = true
     self.tabBarController?.selectedIndex = 1
    }
    

    说明:对于第一种情况,由于您正在寻找的 VC 嵌入在 navigationController 中,那么您已经将方法转换为您正在寻找的 VC 的子节点。对于案例二,没有navigationController,所以你直接投射到你正在寻找的VC,即..不需要像案例1那样找到孩子。

    【讨论】:

    • “NewsViewController”类型的值没有成员“viewControllers”
    • 糟糕,我修正了我的答案,您需要访问该视图的 tabBarController。请检查我的答案,然后再试一次。
    • 什么都没发生,如果让条件不进入
    • 我认为既然你说你想将你的价值传递给 CompetitionVC 那么你应该投给它。以防万一我将 TargetVC 命名为 CompetitionVC 并编辑了我的答案,以便您可以仔细检查您的目标 VC,如果它确实是该索引 1 处的 CompettionVC。
    • 我的两个视图控制器(新闻和比赛)都嵌入了导航控制器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 2019-11-30
    相关资源
    最近更新 更多