在ViewController和TabBarController之间传递数据 [英] Pass data between ViewController and TabBarController

查看:200
本文介绍了在ViewController和TabBarController之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个问题. 如何将数据(在Alamofire请求完成后得到的数据)传递给TabBarController的子级之一?

I have couple of questions. How to pass the data (which i got after the Alamofire request is finished), to one of children of TabBarController?

我遇到的第一个问题是我无法覆盖登录操作中的func prepareForSegue函数(单击该按钮时),它说我只能覆盖类成员.但是,如果我将功能放到IBAction之外,那么我将不会发送所需的数据.

The first problem i have is that i can't override the func prepareForSegue inside login action(when the button is tapped), it says i can override only class members. But if i put the func outside of IBAction then i won't send the data that i need.

第二个问题是,当我将重写的函数放在IBAction之外时,代码如下所示:

And the second problem is, when i put the overrided function outside of IBAction, and the code look like this:

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

            let homeVC = segue.destinationViewController as HomeViewController
            homeVC.templateForCell = templates
        }

当我运行它时,出现错误:

when i run it, i got the error:

无法将类型为"UITabBarController"的值强制转换为 HomeViewController'

Could not cast value of type 'UITabBarController' to HomeViewController'

(HomeViewController是我的目标视图,我应该在其中传递Alamofire的数据).

(HomeViewController is my destination view, where i should pass the data from Alamofire).

推荐答案

您不必为此使用prepareForSegue.只需在TabBarController viewControllers数组中引用所需的ViewController并将其强制转换即可.

You don't necessarily need to use prepareForSegue for this. Just reference which ViewController in the TabBarController viewControllers array that you want and cast it.

let vc = self.tabBarController.viewControllers![1] as! HomeViewController
vc.templateForCell = templates

如果TabBar中的ViewControllers嵌入在Navigation Controllers中,则可以执行以下操作:

If the ViewControllers in your TabBar are embedded in Navigation Controllers, you can do this:

let navController = self.tabBarController.viewControllers![1] as! UINavigationController
let vc = navController.topViewController as! HomeViewController
vc.templateForCell = templates

这篇关于在ViewController和TabBarController之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆