2017-05-24 59 views
0

我试图从一个VC(称为第二个VC)到另一个VC(第一个VC)传递一个文本值。第二个VC中的信息将决定第一个VC中文本字段的文本值。我曾尝试使用文档来解决这个问题,但我必须变得非常困惑或者其他的东西已经起来 - 我无法弄清楚。Swift 3.0使用Segue在视图控制器之间传递信息

在我secondVC:

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) 
} 

segueing回firstVC后,文本框不与搜索栏的文字如预期填写。 (searchBar文本不是零)根据我的理解,当在第二个VC中选择按钮并调用performSegue时,它还会调用prepareForSegue并设置addressTextField.text值,同时继续回到第一个VC。我不确定我的逻辑是否正确。

+0

你使用的是navigationController吗?你确定你实际上是在寻找'firstVC',而不是从naviationController栈中弹出'secondVC'吗?在'prepareForSegue'中放置一个断点,看它是否实际被调用。 – toddg

+0

我没有使用导航控制器 - 只有情节串联。我在prepareForSegue中设置了一个断点,但它没有被调用。这两个有关系吗? – Kevin

+2

通常,当你要回退到前一个viewController时,你实际上并没有执行一个segue。您要么关闭模式视图segue,要么从viewController堆栈中弹出视图(如果您使用的是navigationController)。传回数据有点复杂,我建议的做法是与代表一起进行。好信息[这里](https://medium.com/ios-os-x-development/pass-data-with-delegation-in-swift-86f6bc5d0894) – toddg

回答

1

在你FirstViewController

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

在你secondViewController

var searchedText = "" 


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

现在连接您的viewController从下拉

退出在上面,添加一个标识符,该赛格瑞作为saveSegue并选择dismissSearchVC

enter image description here

enter image description here

然后从这里选择赛格瑞 enter image description here

现在选择“放松顺着接下去DismissSearchVC”,并在属性检查器中添加标识..

+1

我的prepareForSegue甚至没有调用,所以这是行不通的 – Kevin

+2

其实你正在做一个流行或解雇viewController所以它不叫... 等待我编辑我的答案 –

+0

addressTextField是在firstViewController,所以在secondViewController viewDidLoad,addressTextField.text = searchingText产生一个错误 – Kevin

0

我怀疑从一个视图控制器到另一个你的过渡视图控制器没有发生,因为segue,而是因为按钮的连接而发生。

删除您从UIButton连接到AnotherViewController所连接的segue

现在从包含ViewController的按钮中添加segueAnotherViewController。 (不是从UIButtonAnotherViewController,而是从ViewControllerAnotherViewController)。并给你segue你的预期标识符。

相关问题