ios - 返回上一个屏幕时如何在 Controller 之间传输数据? swift

标签 ios swift realm

在应用程序中,我被要求选择一个类别,当我点击时,我从 tableView 移动到下一个屏幕,当我点击所需的类别时,它应该被保存并返回到之前的 Controller ,在那里而不是“选择一个类别”,我选择的类别将是。

我通过此方法使用 navigationController?.popViewController(animated: true),这让我回到了最后一个屏幕。 据我了解,未调用 prepare,但由于我确切地知道我要去哪里,所以我可以在我调用 pop ... 的方法中访问我要切换到的 Controller 并向其传递必要的信息特性?但是如何呢?

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  let currentCell = categories[indexPath.row]
    /?
    /?
  navigationController?.popViewController(animated: true)
}

最佳答案

当你进入选择屏幕时做

let vc = ///
vc.delegate = self
// push

然后在 didASelectRowAt

里面
class CategoryViewController:UIViewController {
 weak var delegate:PreviousVC?
 ////
}

delegate?.sendData(someData)
navigationController?.popViewController(animated: true)

didASelectRowAt 中的另一个选项

// access previous vc
let previousVC = self.navigationController!.viewControllers[self.navigationController!.viewControllers.count - 2] as! PreviousVc
previousVC.sendData(someData)
// pop here

编辑:在第一个VC中

     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let secondVC = segue.destination as? SecondViewController {
          secondVC.delegate = self   
        }
    }

关于ios - 返回上一个屏幕时如何在 Controller 之间传输数据? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56041750/

相关文章:

ios - 无法分析 iOS 应用程序 : permission to debug was denied

iphone - 新请求加载完成后,如何禁用 UIWebView 的自动 "scroll to top"?

swift - 模块最小部署目标捕手

ios - AVD_loader.cpp : failed to get a service for display 4

ios - CGContextAddArc逆时针而不是顺时针

ios - In-Memory Realm 是否有 Swift 中磁盘持久化 Realm 数据的副本?

ios - 为什么 Realm addOrUpdateObject 会导致属性设置为 nil?

javascript - Realm-js 和 Node Js : application freezes

ios - 核心数据将 objectID 传递给另一个线程

ios - 将使用android_studio制作的android应用程序转换为IOS