简体   繁体   English

SWIFT,ViewControllers并将序列展开到初始ViewController

[英]SWIFT, ViewControllers and unwind segue to initial ViewController

I'm having trouble with Unwind Segue. 我在Unwind Segue遇到问题。 I have 3 ViewControllers and they all under the class called ViewController.swift . 我有3个ViewController,它们都在名为ViewController.swift的类下 I'm trying to get the last ViewController which is the 3rd to to go the beginning ViewController with just a tap of a button but I can't seem to get it. 我正在尝试获取最后一个ViewController,这是第三个只需单击一个按钮即可开始使用ViewController的方法,但我似乎无法获得它。 I tried to research before submitting this question but couldn't find it. 在提交此问题之前,我曾尝试进行研究,但找不到。 If there is more information you need let me know. 如果您需要更多信息,请告诉我。

You have VC1 -> VC2 -> VC3 and want to unwind from VC3 directly to VC1. 您具有VC1-> VC2-> VC3,并希望从VC3直接展开到VC1。 In order to do this, you need an @IBAction defined in VC1 that isn't defined in VC2. 为此,您需要在VC1中定义的@IBAction ,而在VC2中未定义。 That is currently not possible since all of your ViewControllers are using the same class definition in ViewController.swift . 既然所有的ViewControllers的使用在ViewController.swift同一类的定义,目前不可能的。

One way to resolve this without duplicating all of the code in ViewController.swift is to subclass ViewController and add just the function that is unique to VC1. 解决此问题而不复制ViewController.swift中所有代码的一种方法是子类化ViewController并仅添加VC1特有的功能。 Then in the Storyboard, select the first ViewController and in the Identity Inspector set the Custom Class to MainViewController . 然后在情节提要中,选择第一个ViewController,然后在Identity Inspector中将Custom Class设置为MainViewController

class MainViewController: ViewController {
    @IBAction func returnToMain(segue: UIStoryboardSegue) {
        print("back in the MainViewController")
    }
}

Then, when you set up your unwind segue , control -drag from your button to the Exit icon at the top of your 3rd ViewController and select the returnToMain action from the pop-up. 然后,当您设置展开顺序时, 控制-拖动按钮到您的第三个ViewController顶部的Exit图标,然后从弹出窗口中选择returnToMain操作。

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

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