简体   繁体   English

使用故事板解开Xcode 6.1.1中的Segue

[英]Unwind Segue in Xcode 6.1.1 with storyboard

I have been reading that unwind segue's are bugged in Xcode 6. I am using Xcode 6.1.1 and I use swift. 我一直在阅读在Xcode 6中使用了解开segue的问题。我正在使用Xcode 6.1.1并使用swift。

I use the "Back" button that is put by the navigation controller. 我使用导航控制器放置的“后退”按钮。 Thus I can't cntrl drag to the exit icon. 因此我无法cntrl拖动到退出图标。 Moreover I can't drag from viewController icon to exit icon either. 此外,我无法从viewController图标拖动到退出图标。

Is this a bug? 这是一个错误吗? Or else I am missing some fundamental knowledge about how to use unwind segue. 或者我错过了一些关于如何使用unwind segue的基本知识。 How can I set unwind segues from the storyboard? 如何从故事板中设置展开segue?

I want to provide a detailed answer: 我想提供一个详细的答案:

To perform an unwind segue, go to the view controller that you want to segue to and add the following function; 要执行展开segue,请转到要转移到的视图控制器并添加以下功能; (the name can change of course) (名称可以改变)

@IBAction func unwindToMainMenu(segue: UIStoryboardSegue) {

}

Once you add this function, you will be able to see this unwind function from any view controller on your storyboard. 添加此功能后,您将能够从故事板上的任何视图控制器中看到此展开功能。 Just right click on the exit icon on the top of any view controller. 只需右键单击任何视图控制器顶部的退出图标即可。 退出图标右键单击

If you want to perform unwind with a button, then you can cntrl + drag from the button to exit icon and choose your unwindSegue function. 如果要使用按钮执行放松,则可以从按钮中unwindSegue键并拖动以退出图标并选择unwindSegue函数。 Done! 完成!

用按钮放松

If you want to perform unwind programmatically, then cntrl + drag from the viewController icon to the exit icon and choose the unwind function. 如果要以编程方式执行展开,则cntrl +从viewController图标拖动到退出图标并选择展开功能。

在此输入图像描述

Afterwards, open the Document Outline and click on the unwind segue. 然后,打开文档大纲并单击展开segue。

在此输入图像描述

Go to Attributes Inspector inside Utilities and give an identifier to your unwind segue. 转到Utilities中的Attributes Inspector,并为unwind segue提供一个标识符。

在此输入图像描述

Lastly, call the performSegueWithIdentifier function like the following; 最后,调用performSegueWithIdentifier函数,如下所示:

self.performSegueWithIdentifier("goToMainMenu", sender: self)

Hope that helps! 希望有所帮助!

Got it working. 搞定了。 If you are presenting from a NavigationController or TabBarController(unsure about the TabBarController), you need to subclass the navigation controller and add the unwind segue to that. 如果您使用的是NavigationController或TabBarController(不确定TabBarController),则需要子类化导航控制器并将unwind segue添加到该控制器。 In storyboard don't forget to change the class of your navigation controller. 在故事板中不要忘记更改导航控制器的类。 My view hierarchy was NavController -> TableController -> DetailController. 我的视图层次结构是NavController - > TableController - > DetailController。 After adding method to custom NavBar class add it to the VC you want to RETURN to. 将方法添加到自定义NavBar类后,将其添加到要返回的VC。 You will then be able to ctrl-drag to exit. 然后,您可以按住Ctrl键拖动退出。 I consider this a bug, as once it is hooked up I was able to delete the subclass and return to stock NavigationController and it still worked. 我认为这是一个错误,因为一旦它被连接,我能够删除子类并返回库存NavigationController,它仍然有效。

Got it working on Xcode 6.2. 得到它在Xcode 6.2上工作。 My case is ViewController A presenting B modally. 我的情况是ViewController A以模态方式呈现B. Unwind setup steps below: 展开以下设置步骤:

  1. In A, write an IBAction 在A中,写一个IBAction

     -(IBAction)unwindFromB:(UIStoryboardSegue *) unwindSegue { ViewControllerB *vc = [unwindSegue sourceViewController]; // get whatever data you want to pass back from B } 
  2. In storyboard, find B and right-click the 'Exit' button at the top, then the 'Presenting Segue' menu will list unwindFromB you've just created in A 在故事板中,找到B并右键单击顶部的“退出”按钮,然后“呈现Segue”菜单将列出您刚刚在A中创建的unwindFromB

  3. Click the little '+' sign to the right and link it back to B, select 'manual' 点击右边的小“+”符号并将其链接回B,选择“手动”

  4. Don't forget to set identifier of the unwind segue if you need to call it programmatically. 如果需要以编程方式调用,请不要忘记设置展开segue的标识符。

I found that I could not drag from the UITableViewCell to the Exit object on the View Controller on the Storyboard. 我发现我无法从UITableViewCell 拖到 Storyboard上View Controller上的Exit对象。

Solution was to first add an appropriate IBAction in code. 解决方案是首先在代码中添加适当的IBAction。 Once an IBAction exists, the drag target will light up. 一旦存在IBAction,拖动目标将亮起。

-(IBAction)prepareForUnwind:(UIStoryboardSegue *)segue {

}

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

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