准备转至 UITabBarController 选项卡
Prepare for Segue to UITabBarController Tab
我想将信息从 ViewController
传递到 UITabBarController
以便我可以从 UITabBarController
的第三个选项卡中的视图控制器访问某些信息。
但是,问题是当我尝试这样做时我的程序总是崩溃。到目前为止,这是我的代码:
我这样调用 segue:
self.firstName = user.first_name
self.lastName = user.last_name
self.performSegueWithIdentifier("OffersView", sender: self)
然后我覆盖了 prepareForSegue
函数,这样我就可以像这样在这个函数中传递信息:
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
(在上面代码的第二行)时,我的代码崩溃了。我不确定为什么,因为我查看了许多 Whosebug 帖子,例如
Swift tab bar view prepareforsegue and Pass data from tableview to tab bar view controller in Swift.但没有太大的成功。我可能需要创建一个 UITabBarControllerDelegate
class 并通过它传递信息吗?任何提示将不胜感激。谢谢!
讨论后发现问题是tab bar controller的children嵌入到navigation controller中,所以代码需要改成这样,
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
}
在 swift 3, xcode 8 中,代码会像
let barViewControllers = segue.destination as! UITabBarController
let nav = barViewControllers.viewControllers![0] as! UINavigationController
let destinationViewController = nav.viewControllers[0] as! YourViewController
destinationViewController.varTest = _varValue
Swift 4 没有强制可选展开的解决方案
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let barVC = segue.destination as? UITabBarController {
barVC.viewControllers?.forEach {
if let vc = [=10=] as? YourViewController {
vc.firstName = self.firstName
vc.lastName = self.lastName
}
}
}
}
我想将信息从 ViewController
传递到 UITabBarController
以便我可以从 UITabBarController
的第三个选项卡中的视图控制器访问某些信息。
但是,问题是当我尝试这样做时我的程序总是崩溃。到目前为止,这是我的代码:
我这样调用 segue:
self.firstName = user.first_name
self.lastName = user.last_name
self.performSegueWithIdentifier("OffersView", sender: self)
然后我覆盖了 prepareForSegue
函数,这样我就可以像这样在这个函数中传递信息:
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
(在上面代码的第二行)时,我的代码崩溃了。我不确定为什么,因为我查看了许多 Whosebug 帖子,例如
Swift tab bar view prepareforsegue and Pass data from tableview to tab bar view controller in Swift.但没有太大的成功。我可能需要创建一个 UITabBarControllerDelegate
class 并通过它传递信息吗?任何提示将不胜感激。谢谢!
讨论后发现问题是tab bar controller的children嵌入到navigation controller中,所以代码需要改成这样,
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
}
在 swift 3, xcode 8 中,代码会像
let barViewControllers = segue.destination as! UITabBarController
let nav = barViewControllers.viewControllers![0] as! UINavigationController
let destinationViewController = nav.viewControllers[0] as! YourViewController
destinationViewController.varTest = _varValue
Swift 4 没有强制可选展开的解决方案
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let barVC = segue.destination as? UITabBarController {
barVC.viewControllers?.forEach {
if let vc = [=10=] as? YourViewController {
vc.firstName = self.firstName
vc.lastName = self.lastName
}
}
}
}