......
do {
try self.managedObjectContext.save()
appUserID = user.userID!
} catch {
fatalError("Error: \(error)")
}
}
}
self.passManagedObjectContext()
}
func passManagedObjectContext() {
let profileTableViewController = UIStoryboard(name: "Profile", bundle: nil).instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileTableViewController
profileTableViewController.managedObjectContext = self.managedObjectContext
}
I am passing data from the AppDelegate to other ViewControllers by referencing my TabBarViewControllers stack like so:
let tabBarController = window!.rootViewController as! UITabBarController
if let tabBarViewControllers = tabBarController.viewControllers {
let profileNavController = tabBarViewControllers[4] as! UINavigationController
let profileTableViewController = profileNavController.topViewController as! ProfileTableViewController
profileTableViewController.managedObjectContext = context
}
}
.....
结束编辑
我现在尝试做同样的事情,但从一个ViewController到另一个通过标签栏访问(没有segue)。上面的代码无法识别window!.rootViewController
如何引用根UITabBarController?
答案 0 :(得分:0)
UIViewController
有一个内置的可选属性tabBarController
,因此您可以在视图控制器中访问tabBarController
,如下所示:
if let tabBarController = tabBarController {
//get the controller you need from
//tabBarController.viewControllers
//and do whatever you need
}
以下是Apple's UIViewController documentation的“获取其他相关视图控制器”部分中可用的当前属性列表: