DestinationViewController Segue 和 UINavigationController swift
DestinationViewController Segue and UINavigationController swift
所以我有一个像这样的 prepareForSegue 方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "fromEventTableToAddEvent" {
let addEventViewController: AddEventViewController = segue.destinationViewController as! AddEventViewController
addEventViewController.newTagArray = newTagArray
}
}
但是,在运行时应用程序崩溃并且日志吐出此消息:
Could not cast value of type 'UINavigationController'
错误出现是因为 addEventController 嵌入在导航控制器中,但是我不确定如何设置 segue 以便将 destinationViewController 设置为 NavigationController,但也允许我在addEventController
的 prepareForSegue 方法
那么我该如何使用 Swift 来做到这一点?
非常感谢您提供的帮助和时间作为回应。很有帮助
您只需要访问导航控制器的topViewController,即AddEventViewController。这是代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "fromEventTableToAddEvent" {
let nav = segue.destinationViewController as! UINavigationController
let addEventViewController = nav.topViewController as! AddEventViewController
addEventViewController.newTagArray = newTagArray
}
}
所有明确需要的是从 segue.destination class 创建一个对象并将其向下转换为您想要的 ViewController.
所以我有一个像这样的 prepareForSegue 方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "fromEventTableToAddEvent" {
let addEventViewController: AddEventViewController = segue.destinationViewController as! AddEventViewController
addEventViewController.newTagArray = newTagArray
}
}
但是,在运行时应用程序崩溃并且日志吐出此消息:
Could not cast value of type 'UINavigationController'
错误出现是因为 addEventController 嵌入在导航控制器中,但是我不确定如何设置 segue 以便将 destinationViewController 设置为 NavigationController,但也允许我在addEventController
的 prepareForSegue 方法那么我该如何使用 Swift 来做到这一点?
非常感谢您提供的帮助和时间作为回应。很有帮助
您只需要访问导航控制器的topViewController,即AddEventViewController。这是代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "fromEventTableToAddEvent" {
let nav = segue.destinationViewController as! UINavigationController
let addEventViewController = nav.topViewController as! AddEventViewController
addEventViewController.newTagArray = newTagArray
}
}
所有明确需要的是从 segue.destination class 创建一个对象并将其向下转换为您想要的 ViewController.