使用导航控制器在“prepareForSegue"中设置数据 [英] Set data in `prepareForSegue` with navigation controller

查看:17
本文介绍了使用导航控制器在“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"中设置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆