我有一个Login项目。第一个视图控制器是NavController,我需要将用户数据传递给tabBarController,我有3个NavigationControllers。
我试过了。
let openNewVC = self.storyboard?.instantiateViewControllerWithIdentifier("mainNavID") as! UITabBarController
//openNewVC.token = token!
self.navigationController?.pushViewController(openNewVC, animated: true)
答案 0 :(得分:1)
您可以尝试将值设置为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)
答案 1 :(得分:1)
您可以将第一个视图控制器中的数据传递给查看嵌入式控制器,如UITabbarController
中所示。
UITabBarController -> UINavigationController -> UIViewController
您需要遍历viewControllers
和UITabBarController
的{{1}}实例,才能获得UINavigationController
的实例。一旦您获得嵌入UIViewController
的'UIViewController'实例,您就可以根据需要分配数据。
例如
UITabbarController
使用Singleton在这种架构中传递数据的最佳方法,假设您创建了一个类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 {
}
}
}
}
,其成员变量为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