【问题标题】:How to pass value from navigationController to TabBarController如何将值从导航控制器传递到 TabBarController
【发布时间】:2015-10-27 16:19:13
【问题描述】:

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

我试过了。

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

【问题讨论】:

    标签: swift uitabbarcontroller swift2 ios9


    【解决方案1】:

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

    UITabBarController -> UINavigationController -> UIViewController

    需要遍历UITabBarControllerUINavigationControllerviewControllers实例,才能得到UIViewController的实例。一旦您获得嵌入到UITabbarController 中的“UIViewController”实例,您就可以根据需要分配数据。

    举例

    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

    class Session {
    
        //Singleton
        static let sharedInstance = Session()
    
        //Token which you assign and you can use through out application
        var token : String? = nil
    }
    

    现在,您可以在推送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
        }
    }
    

    【讨论】:

    • 单例是我使用的方式??
    【解决方案2】:

    您可以尝试将值设置为openNewVC,就像设置普通属性一样。

    示例:

    //set instance
    var openNewVC = self.storyboard?.instantiateViewControllerWithIdentifier("mainNavID") as! UITabBarController
    
    // set properties
    openNewVC.myFancyString = "Hello world!"
    
    // set view controller active
    self.navigationController?.pushViewController(openNewVC, animated: true)
    

    【讨论】:

    • 嗯,我想要的是。将数据从 mainVC 传递到位于 TabBar Tree 中的 VC。您的代码正是我尝试过的,但失败了,因为我没有 TabBarCONtroller 的自定义类。使用此代码,我可以访问目标 VC,但没有 TabBar 底部。
    • 可以自己写 TabBarVC 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多