准备Segue到UITabBarController选项卡 [英] Prepare for Segue to UITabBarController Tab

查看:86
本文介绍了准备Segue到UITabBarController选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将信息从 ViewController 传递到 UITabBarController ,以便我可以从视图中访问某些信息 UITabBarController 的第3个选项卡中的控制器。

I want to pass information from a ViewController to a UITabBarController so that I can access certain information from the view controller in the 3rd tab of the UITabBarController.

然而,问题是我的程序在我尝试时仍然崩溃这样做。这是我到目前为止的代码:

However, the issue is my program keeps crashing when I try to do so. Here is my code so far:

我像这样调用segue:

I call the segue like so:

self.firstName = user.first_name
self.lastName = user.last_name
self.performSegueWithIdentifier("OffersView", sender: self)

我覆盖 prepareForSegue 函数,以便我可以像这样传递这个函数中的信息:

And I override the prepareForSegue function so that I can pass information in this function like so:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if(segue.identifier == "OffersView"){
        let barViewControllers = segue.destinationViewController as UITabBarController
        let destinationViewController = barViewControllers.viewControllers![2] as ProfileController
        destinationViewController.firstName = self.firstName
        destinationViewController.lastName = self.lastName
    }
}

当我尝试设置 destinationViewController (在代码的第二行上面),我的代码崩溃了。我不知道为什么我看了很多StackOverflow帖子,例如

When I try to set the destinationViewController (on the second line in the code above), my code crashes. I'm not sure why as I've looked on numerous StackOverflow posts such as

Swift标签栏查看prepareforsegue 将数据从tableview传递到Swift中的标签栏视图控制器。但没有取得多大成功。我是否可能需要创建一个 UITabBarControllerDelegate 类并通过它传递信息?任何提示将不胜感激。谢谢!

Swift tab bar view prepareforsegue and Pass data from tableview to tab bar view controller in Swift. but have not have much success. Do I possibly need to create a UITabBarControllerDelegate class and pass information through that? Any tips would be appreciated. Thanks!

推荐答案

讨论后发现的问题是标签栏控制器的子项嵌入在导航控制器中,所以代码需要更改为,

The problem, as discovered after a discussion, was that the tab bar controller's children were embedded in navigation controllers, so the code needed to be changed to this,

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    let barViewControllers = segue.destinationViewController as! UITabBarController 
    let nav = barViewControllers.viewControllers![2] as! UINavigationController 
    let destinationViewController = nav.topviewcontroller as ProfileController 
    destinationViewController.firstName = self.firstName
    destinationViewController.lastName = self.lastName 
}

这篇关于准备Segue到UITabBarController选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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