Set data in `prepareForSegue` with navigation controller(使用导航控制器在“prepareForSegue中设置数据)
问题描述
我正在用 Swift 开发一个 iOS 应用程序.
I am developing an iOS application in Swift.
我想使用 prepareForSegue
函数将数据从一个视图发送到另一个视图.
I want to send data from a view to an other one, using the prepareForSegue
function.
但是,我的目标视图前面有一个导航控制器,所以它不起作用.如何在导航控制器中包含的 VC 上设置数据?
However, my target view is preceded by a navigation controller, so it doesn't work. How can I set data on the VC contained within the navigation controller?
推荐答案
在prepareForSegue
中访问目标导航控制器,然后访问其顶部:
In prepareForSegue
access the target navigation controller, and then its top:
let destinationNavigationController = segue.destination as! UINavigationController
let targetController = destinationNavigationController.topViewController
您可以从目标控制器访问其视图并传递数据.
From the target controller you can access its view and pass data.
在旧版本 - 现在已过时 - Swift 和 UIKit 版本中,代码略有不同:
In old - now obsolete - versions of Swift and UIKit the code was slightly different:
let destinationNavigationController = segue.destinationViewController as UINavigationController
let targetController = destinationNavigationController.topViewController
这篇关于使用导航控制器在“prepareForSegue"中设置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!