ios - 以编程方式设置 Storyboard ID
问题描述
我在一个团队中工作,我需要的一个故事板是以编程方式完成的,这很好。我目前正在尝试使用委托将数据从一个视图控制器(以编程方式完成的 VC)传递到另一个(界面构建器构建的故事板)。
一个 VC(发件人)的代码是:
protocol setDelegate {
func passData(_ area: String, _ location: String)
}
class ProgramaticVC : UIViewController {
var delegate: setDelegate?
...
func passData() {
self.delegate?.passData(area, location)
let interfaceBuilderVC = UIStoryboard.init("Main", nil).instantiateViewController("interfaceBuilderVC") as! InterfaceBuilderVC
show(interfaceBuilderVC, nil)
}
接收方代码:
class InterfaceBuilderVC : UIViewController {
...
func passData(_ area: String, _ location: String) {
areaLabel.text = area
locationLabel.text = location
// Not sure how to set the delegate, but what I was thinking:
let programmaticVC = UIStoryboard.init("Main", nil).instantiateViewController("CAN'T SET ID") as! ProgrammaticVC
programmaticVC.delegate = self
}
所以我要么希望以编程方式设置情节提要的 ID,要么就如何正确设置我的委托获得任何其他建议。
解决方案
如果您只想将数据从一个视图控制器发送到另一个视图控制器,则没有理由使用委托。你可以简单地这样做:
class ProgramaticVC : UIViewController {
func passData() {
let interfaceBuilderVC = UIStoryboard.init("Main", nil).instantiateViewController("interfaceBuilderVC") as! InterfaceBuilderVC
interfaceBuilderVC.area = area
interfaceBuilderVC.location = location
show(interfaceBuilderVC, nil)
}
}
和
class InterfaceBuilderVC : UIViewController {
var area: String?
var location: String?
}
InterfaceBuilderVC
如果您需要与 进行交流,您可能想要使用委托ProgramaticVC
,但您的问题并不清楚这就是您想要做的。
推荐阅读
- c# - 将项目列表分解为可用于表示它的最小组数的算法
- syslog - 将系统日志客户端主机名添加到流利位事件
- python - 使用 django rest auth 和 mongodb 登录
- java - 在 JPA 存储库 Spring Boot 应用程序中调用 Postgres 存储过程
- ios - 使用 xcframeworks 运行我的项目时出现脚本错误“从 ArcGIS 致命错误中提取 arm64:lipo:输入文件”
- php - php工匠路线:列出错误异常
- ios - Xcode 12.5 调试器自 macOS Big Sur 11.3 起运行缓慢(本机 Swift 项目)
- java - 如何在java中提取前两个字符和后两个字符之间的字符串?
- time - 如何验证时间是否在SSRS中的时间范围内
- javascript - 无法合并两个对象数组