如何将值从navigationController传递到TabBarController [英] How to pass value from navigationController to TabBarController

查看:88
本文介绍了如何将值从navigationController传递到TabBarController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Login项目.第一个视图控制器是NavController,我需要将用户数据传递到我有3个NavigationControllers的tabBarController.

I have a Login project. The first view controller is NavController and I need to pass user data to tabBarController where I have 3 NavigationControllers.

我尝试过这个.

let openNewVC = self.storyboard?.instantiateViewControllerWithIdentifier("mainNavID") as! UITabBarController
//openNewVC.token = token!
self.navigationController?.pushViewController(openNewVC, animated: true)

推荐答案

您可以将数据从您的第一个视图控制器传递到如下所示的嵌入视图控制器中,如UITabbarController所示.

You can pass the data from your first view Controller to view controllers which are embedded like as below in UITabbarController.

UITabBarController -> UINavigationController -> UIViewController

您需要遍历UITabBarControllerUINavigationControllerviewControllers实例以获取UIViewController的实例.一旦获得嵌入到UITabbarController中的'UIViewController'实例,就可以根据需要分配数据.

You need to traverse the viewControllers instance of UITabBarController and UINavigationController in order to get the instance of UIViewController. Once you will get the instance of 'UIViewController' which is embedded into UITabbarController, you can assign the data as required.

例如

if let tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("mainNavID") as? UITabBarController {

    // Now you need to get View Controllers array from tabBarControllers which is UINavigationControllers
    if let navigationControllers = tabBarController.viewControllers as? [UINavigationController] {
    for navigationController in navigationControllers {
        //Now you need to get the viewControllers from navigationController stack,

         let viewControllers = navigationController.viewControllers

        //Now you can assing desired value in viewControllers, I am assuming you need to assign the same value in all viewControler
        for viewController in viewControllers {

        }
        }
    }
    }

使用Singleton在这种体系结构中传递数据的最佳方法,假设您创建了一个类Session,该类的成员变量token.

The best way to pass the data in this kind of architecture using Singleton, Assume you created a class Session which member variable token.

class Session {

    //Singleton
    static let sharedInstance = Session()

    //Token which you assign and you can use through out application
    var token : String? = nil
}

现在,您可以在按下UITabbarController的同时分配令牌.

Now, you can assign the token while pushing the UITabbarController.

 let openNewVC = self.storyboard?.instantiateViewControllerWithIdentifier("mainNavID") as! UITabBarController
Session.sharedInstance.token = token    
//openNewVC.token = token!
self.navigationController?.pushViewController(openNewVC, animated: true)

您可以在UIViewController

override func viewDidLoad() {
    super.viewDidLoad()

    if let token = Session.sharedInstance.token {
        //Now you have assigned token
    }
}

这篇关于如何将值从navigationController传递到TabBarController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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