Swift 3.0 使用 Segue 在 View Controller 之间传递信息

标签 swift segue uistoryboardsegue

我正在尝试将文本值从一个 VC(称为第二个 VC)传递到另一个 VC(第一个 VC)。 secondVC 中的信息将决定 firstVC 中文本字段的文本值。我曾尝试使用文档来解决这个问题,但我一定感到非常困惑,或者出现了其他问题 - 我无法弄清楚是哪个。

在我的第二个 VC 中:

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if(segue.identifier == "saveSegue") {

        let destinationVC = segue.destination as! firstVC

        destinationVC.addressTextField.text = searchBar.text
    }
}


@IBAction func savePressed(_ sender: Any) {


    performSegue(withIdentifier: "saveSegue", sender: self)
}

回到第一个 VC 后,textField 没有按预期填充 searchBar 文本。 (searchBar 文本不是零)据我了解,当在第二个 VC 中选择按钮并调用 performSegue 时,它​​还会调用 prepareForSegue 并设置 addressTextField.text 值,同时返回到第一个 VC。我不确定我的逻辑是否正确。

最佳答案

在你的 FirstViewController 中

    @IBAction func dismissSearchVC(_ sender: UIStoryboardSegue) {
      let sourceVC = sender.source as! viewController2
      addressTextField.text = sourceVC.searchBar.text
}

在你的第二个ViewController中

var searchedText = ""


@IBAction func savePressed(_ sender: Any) {
    performSegue(withIdentifier: "saveSegue", sender: self)
}

现在连接你的 viewController 以在顶部退出并添加一个标识符到该 segue 作为 saveSegue 并从下拉列表中选择 dismissSearchVC

enter image description here

enter image description here

然后从这里选择你的segue enter image description here

现在选择“unwind segue to DismissSearchVC”并在属性检查器中添加标识符..

关于Swift 3.0 使用 Segue 在 View Controller 之间传递信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44165109/

相关文章:

swift - 如何快速从 UITableView 中删除部分中的页眉 View 和页脚 View ?

ios - 如何在新 Storyboard中以编程方式转至新 ViewController

ios - 使用表格单元格内的按钮从原型(prototype)单元格传递数据

objective-c - Segue 使我的程序崩溃。与我的 NavigationController 和 TabBarViewController 有关

ios - 快速执行 Segue 到另一个 Storyboard

ios - 我可以从 UIBarButtonItem 触发 segue 吗?

ios - 如何使用 URLSession 解析并将它们导入 Swift 中的数组中?

arrays - 比较字符串与字符串数组

swift - 自定义 Cell Segue 将数据发送到第二个 View Controller 不起作用

swift - 尝试在不提示位置授权的情况下启动 MapKit 位置更新