简体   繁体   English

为什么界面构建器没有显示我的展开segue?

[英]Why is interface builder not showing my unwind segue?

My app's navigation flow looks a bit like this: 我的应用程序的导航流程看起来有点像这样:

UINavigationController - MasterViewController > DetailViewController > InfoViewController UINavigationController - MasterViewController> DetailViewController> InfoViewController

MasterViewController contains the following method: MasterViewController包含以下方法:

@IBAction func unwindToMaster(with segue: UIStoryboardSegue) {}

In DetailViewController, there is a similar method: 在DetailViewController中,有一个类似的方法:

@IBAction func unwindToDetail(with segue: UIStoryboardSegue) {}

I use these methods, along with UIButtons, to allow the user to advance forward and back through the navigation hierarchy. 我使用这些方法以及UIButtons来允许用户在导航层次结构中前进和后退。 In interface builder, I see the method under DetailViewController when I right click on "Exit" in InfoViewController, but when I right click on "Exit" in DetailViewController, no other unwind segues are listed. 在界面构建器中,当我右键单击InfoViewController中的“Exit”时,我看到DetailViewController下的方法,但是当我右键单击DetailViewController中的“Exit”时,没有列出其他展开的segues。

I have consulted multiple online sources (Ray Wenderlich, relevant StackOverflow questions) instructing the correct way to produce unwind segues in interface builder, but none of these have helped solve the issue. 我已经咨询了多个在线资源(Ray Wenderlich,相关的StackOverflow问题),指导在界面构建器中生成展开segue的正确方法,但这些都没有帮助解决问题。 Right now, I need help figuring out what the problem is in the first place. 现在,我首先需要帮助找出问题所在。 As development usually goes, it's probably something staring me square in the face. 随着发展的进行,它可能会让我看起来像脸一样。

DetailViewController中的退出菜单 - 'unwindToPersonWith:'='unwindToDetail'

I am running Xcode 8.1 using Swift 3. Thank you. 我正在使用Swift 3运行Xcode 8.1。谢谢。

To re-iterate: not only an Action for a Storyboard Unwind Segue has to be placed in the same source file as class definition for an unwind-to (destination) View Controller (eg, @IBAction func prepareForUnwind(segue: UIStoryboardSegue) , detected as prepareForUnwind[With]Segue in a "Presenting Segues" list), but also that View Controller cannot have ANY extensions in ANY secondary source files . 再次重申:不仅用于故事板开卷Segue公司的动作必须被放置在同一个源文件中class定义为一个退绕到(目标)视图控制器(例如, @IBAction func prepareForUnwind(segue: UIStoryboardSegue)检测到的as prepareForUnwind[With]Segue in“Presenting Segues”列表),但View Controller 也无法在任何辅助源文件中进行任何扩展 You have to merge class definition and all extensions into a single source file. 您必须将class定义和所有扩展合并到单个源文件中。

(As of Xcode 8.2.1.) (自Xcode 8.2.1起)

In my case, I had a complicated inheritance in my view controller and it was a reason why Interface Builder did not see my unwind. 就我而言,我的视图控制器中有一个复杂的继承,这就是为什么Interface Builder没有看到我放松的原因。 So this workaround works for me well: 所以这个解决方法对我很有用:

  1. Go to YouCollViewController.swift file and delete all classes, protocols, generics, etc. your view controller implements in view controller's declaration 转到YouCollViewController.swift文件并删除视图控制器在视图控制器声明中实现的所有类,协议,泛型等
  2. Inherit your view controller from UIViewController UIViewController继承您的视图控制器
  3. Go to the storyboard and connect your action with the unwind , it should appear in unwinds list 转到故事板并将您的操作与unwind ,它应该显示在展开列表中
  4. Go back to YouCollViewController.swift file and discard all the changes 返回YouCollViewController.swift文件并放弃所有更改

I consulted Apple's Developer Library (specifically the page "Using Unwind Segues" ). 我咨询了Apple的开发者库(特别是“使用Unwind Segues”页面)。 There, the example definition of an unwind action is: 在那里,展开动作的示例定义是:

@IBAction func unwindToMainMenu(sender: UIStoryboardSegue) {
    let sourceViewController = sender.sourceViewController
    // Pull any data from the view controller which initiated the unwind segue.
}

Applying this example in my code, I changed my unwind action declarations to: 在我的代码中应用此示例,我将展开操作声明更改为:

@IBAction func unwindToMaster(sender: UIStoryboardSegue) {
    print("Unwinded to master.")
}

and

@IBAction func unwindToDetail(sender: UIStoryboardSegue) {
    print("Unwinded to detail.")
}

I also made sure that each method was contained within the same file as MasterViewController's class declaration. 我还确保每个方法都包含在与MasterViewController的类声明相同的文件中。 After further testing, I found that all extensions of MasterViewController had to exist in the same file for interface builder to find and recognize the unwind segue. 经过进一步测试,我发现MasterViewController的所有扩展都必须存在于同一个文件中,以便界面构建器找到并识别展开segue。

Now, in storyboard, the exit menu shows both unwind segues. 现在,在故事板中,退出菜单显示两个展开的segues。 My conclusion is that by fiddling around with where the methods are placed and how they are declared, a configuration that interface builder can recognize will be found. 我的结论是,通过摆弄方法的位置以及如何声明它们,可以找到界面构建器可以识别的配置。 I hope it will be less touchy in the future, as my current method organization is very long and difficult to navigate. 我希望将来不那么敏感,因为我目前的方法组织很长很难以导航。

在此输入图像描述

Because none of these answers helped me and I didn't want to mess around with my code that much. 因为这些答案都没有帮助我,我不想那么多地使用我的代码。 Here another solution you can give a try. 在这里你可以尝试另一种解决方案。

Starting from the point where you have already added the unwind function to your MasterViewController. 从已经将展开函数添加到MasterViewController的点开始。

  1. First I have gone ahead and added a new ViewController to my storyboard as well as a new Cocoa Touch Class File of type UIViewController and connected it with my Storyboard one (I called it the HelperViewController). 首先,我继续向我的故事板添加一个新的ViewController以及一个类型为UIViewController的新Cocoa Touch类文件,并将其与我的Storyboard连接(我称之为HelperViewController)。

  2. Then you can add the same unwind function, you already have inside your MasterVC to your newly created HelperVC. 然后你可以添加相同的展开功能,你已经在你的MasterVC里面有新创建的HelperVC。

    @IBAction func unwindToMaster(with segue: UIStoryboardSegue) {}

  3. Now connect the Helper to your DetailVC. 现在将Helper连接到DetailVC。 Your Storyboard should look somewhat like mine. 你的故事板应该看起来有点像我的。 You should get the same option as me if you ctrl + drag and drop to the exit of your DetailVC. 如果你按住Ctrl +拖放到DetailVC的出口,你应该得到与我相同的选项。 When the connection has been established and you hover over the newly created segue both the Master and the HelperVC should be highlighted. 建立连接并将鼠标悬停在新创建的segue上时,应突出显示Master和HelperVC。 Now you can delete the HelperVC again and everything should work as expected. 现在您可以再次删除HelperVC,一切都应该按预期工作。

I hope that helped! 我希望有所帮助!

我的故事板

With swift 4 XCode 9.2 I found that you need to drag the reference from the yellow icon at the top of the viewController to the "Exit" reference of the target viewController in the left details panel. 使用swift 4 XCode 9.2,我发现你需要将引用从viewController顶部的黄色图标拖到左侧详细信息面板中目标viewController的“Exit”引用。 Hope this helps! 希望这可以帮助!

有时XCode重启会使操作出现。

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

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