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"中设置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用导航控制器在“prepareForSegue"中设置数据
基础教程推荐
- Play 商店的设备兼容性问题 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
