ios - 如何 popViewController 并发送回数据
问题描述
我有以下代码可以返回最后一个视图控制器
self.navigationController?.popViewController(animated: true)
当我这样做时,如何将数据发送回最后一个视图控制器?
解决方案
Swift 很大程度上依赖于委托模式,这是一个使用它的好地方。
class FirstViewController: UIViewController {
func pushToSecondViewController() {
let second = SecondViewController()
second.firstViewControllerDelegate = self // set value of delegate
navigationController?.pushViewController(second, animated: true)
}
func someDelegateMethod() {
print("great success")
}
}
class SecondViewController: UIViewController {
weak var firstViewControllerDelegate: FirstViewController? // establish a delegate
func goBackToFirstViewController() {
firstViewControllerDelegate?.someDelegateMethod() // call delegate before popping
navigationController?.popViewController(animated: true)
}
}
推荐阅读
- sql - SQL 使用 OFFSET / FETCH 获取每个类别的产品
- java - -bash:从命令行运行 Java 程序时出现意外标记“(”附近的语法错误
- excel - 基于星期几的动态工作计划
- c++ - Qt 5.11,QWidget 和创建窗口并连接到信号
- java - 在java中返回多个单词的字符串中的特定单词
- c# - 如何使用时区格式化 url 日期时间参数
- java - 如何将一个接口用作具有多个实现的其他接口的参数?
- javascript - 反映文件夹结构的 JSON 对象的路径列表
- javascript - PDF打印对话框获取空白页
- javascript - 如何 Promise.all 用于嵌套数组?