简体   繁体   English

xcode 8 swift:展开segue传输数据不起作用

[英]xcode 8 swift: unwind segue transfer data is not working

So I have been trying to get a unwind segue to work from last two days. 因此,我一直在努力争取从最近两天开始工作。 I have tried watching and follow several different tutorials and none was successful. 我尝试观看并遵循几种不同的教程,但都没有成功。 I created the @IBAction override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) but this never performs any of the code. 我创建了@IBAction override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController)但是它从不执行任何代码。 I put a simple print and had it print text, it never printed the text. 我做了一个简单的打印,并打印了文本,但从未打印过文本。 I created that in the first View Controller then in the second View Controller I had a button that I connected to the Exit at the top of the View Controller and selected the unwind action and it never unwinds. 我在第一个View Controller中创建了一个,然后在第二个View Controller中创建了一个按钮,该按钮连接到View Controller顶部的Exit并选择了展开动作,并且它永远不会展开。 What am I doing wrong or missing? 我在做什么错还是想念?

So here is what I have so far I have 2 views: RaceViewController & SingleStatViewController 因此,到目前为止,这里有2个视图:RaceViewController和SingleStatViewController

I will start by selecting specified rows in RaceViewController and they will open SingleStatViewController modally. 我将从在RaceViewController中选择指定的行开始,它们将以模态方式打开SingleStatViewController。 I have a button in SingleStatViewController called dismissBtn. 我在SingleStatViewController中有一个名为dismissBtn的按钮。 When I click the button it dismisses SingleStatViewContoller. 当我单击按钮时,它关闭了SingleStatViewContoller。 I just need it to pass some data back to the RaceViewController. 我只需要将一些数据传回RaceViewController。

So if the user selects 1 of 3 cells in the table (the rows are Half-Orc, Half-Elf and Human) it will open SingleStatViewController and ask to select 1 stat (these will be of 6 buttons (Str, Dex, Con, Int, Wis, Chr) when they select the one of the buttons it will return the data and update the detailTextLabel. 因此,如果用户选择表格中3个单元格中的1个(行为Half-Orc,Half-Elf和Human),则会打开SingleStatViewController并要求选择1个stat(这些将有6个按钮(Str,Dex,Con, Int,Wis,Chr)时,如果选择其中一个按钮,它将返回数据并更新detailTextLabel。

I select Human then I select Str. 我选择“人类”,然后选择“ Str”。 When it gets back to RaceViewController and it updates the text in the detailTextLabel to +2 Str. 当它返回RaceViewController时,它将detailTextLabel中的文本更新为+2 Str。

Right now, this is the code I am using to dismiss. 现在,这是我要关闭的代码。 Can I use this to continue or do I actually need to perform the unwind: 我可以使用它来继续还是实际上需要执行展开操作:

@IBAction func dismissbtn(_ sender: Any)
{
    dismiss(animated: true, completion: nil)
}

You need to perform unwind and send data as following: 您需要执行展开和发送数据,如下所示:

@IBAction func unwindToRaceViewController(sender: UIStoryboardSegue) 
{
    if let sourceViewController = sender.sourceViewController
                            as? RaceViewController {
    stat = sourceViewController.stat
}

You could refer to this article for unwind segues as alternative to delegate 您可以参考本文以了解更多信息,以代替委托

You could also use delegates to pass data back to the previous controller. 您还可以使用委托将数据传递回前一个控制器。

Okay, so you have vc A and vc B, this is what you need to do to use unwind segue: 好的,所以您拥有vc A和vc B,这是使用unwind segue所需要做的:

  1. In vc A, create a segue function eg. 在vc A中,创建一个segue函数,例如。 @IBAction func unwind(segue: UIStoryboardSegue)

  2. In vc B, write code that call self.performSegueWithIdentifier("unwind", sender: self) (assume that the identifier and the function name is the same - call this in your dismissbtn ) 在vc B中,编写调用self.performSegueWithIdentifier("unwind", sender: self) (假定标识符和函数名称相同-在dismissbtn调用此dismissbtn

  3. In vc B in storyboard, look on top of it, there will be an yellow button and 2 orange button, hold and drag the yellow button to the Exit orange button, choose the action that you created in vc A, give it an identifier as mentioned 在情节提要中的vc B中,在其顶部查看,将有一个黄色按钮和2个橙色按钮,按住并将黄色按钮拖动到“退出”橙色按钮,选择您在vc A中创建的操作,并为其指定标识符提到

  4. In the unwind func, the rest is the same as normal segue when you pass data forward, just change destination to source and you must present vc B from vc A, else then use delegate unwind功能中,其余部分与正常传递时相同,只是将destination更改为source并且必须从vc A中提供vc B,否则再使用委托

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM