Ios 无法将数据传递到导航栏中嵌入的视图控制器
我正在尝试这样做: 我想将一些数据传递给内嵌在导航栏中的ApplicationViewController,因此出现错误:无法强制转换类型为“UINavigationController”的值Ios 无法将数据传递到导航栏中嵌入的视图控制器,ios,swift,xcode,Ios,Swift,Xcode,我正在尝试这样做: 我想将一些数据传递给内嵌在导航栏中的ApplicationViewController,因此出现错误:无法强制转换类型为“UINavigationController”的值 我无法理解如何实现同样的目标。非常感谢你的帮助。谢谢。原因是您无法将数据直接传递到ApplicationViewController,因为InboxViewControllersegue的目标是UINavigationController 因此,首先需要从堆栈访问UINavigationControll
我无法理解如何实现同样的目标。非常感谢你的帮助。谢谢。原因是您无法将数据直接传递到
ApplicationViewController
,因为InboxViewController
segue的目标是UINavigationController
因此,首先需要从堆栈访问UINavigationController
,然后访问ApplicationsViewController
请尝试以下代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToApplications" {
if let navigation = segue.destination as? UINavigationController, let applicationVC = navigation.topViewController as? ApplicationsViewController {
applicationVC.id = "RGB"
}
}
}
原因是您无法将数据直接传递到
ApplicationViewController
,因为InboxViewController
segue的目标是UINavigationController
因此,首先需要从堆栈访问UINavigationController
,然后访问ApplicationsViewController
请尝试以下代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToApplications" {
if let navigation = segue.destination as? UINavigationController, let applicationVC = navigation.topViewController as? ApplicationsViewController {
applicationVC.id = "RGB"
}
}
}
当传递数据应用程序视图控制器时,该控制器是UINavigationController的rootviewController。您应该给出导航控制器的segue名称,并键入cast as UINavigationController as destination viewcontroller。 获取作为应用程序视图控制器的rootviewController并分配id的值 下面是如何将值分配给应用程序ViewController的代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "navigationSegue" { // segue of navigationVC
let navVc = segue.destination as! UINavigationController
let appVc = navVc.viewControllers.first as! ApplicationsViewController
appvc.id = "RGB"
}
}
当传递数据应用程序视图控制器时,该控制器是UINavigationController的rootviewController。您应该给出导航控制器的segue名称,并键入cast as UINavigationController as destination viewcontroller。 获取作为应用程序视图控制器的rootviewController并分配id的值 下面是如何将值分配给应用程序ViewController的代码
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "navigationSegue" { // segue of navigationVC
let navVc = segue.destination as! UINavigationController
let appVc = navVc.viewControllers.first as! ApplicationsViewController
appvc.id = "RGB"
}
}
请在屏幕截图中添加有问题的“文本”代码。请在屏幕截图中添加有问题的“文本”代码。