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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27295607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:18:42  来源:igfitidea点击:

Unwind Segue in Xcode 6.1.1 with storyboard

iosxcodexcode6

提问by Berkan Ercan

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 中的 unwind 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. 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. How can I set unwind segues from the storyboard?

这是一个错误吗?否则我缺少一些关于如何使用 unwind segue 的基本知识。如何从情节提要中设置展开转场?

采纳答案by hidden-username

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. In storyboard don't forget to change the class of your navigation controller. My view hierarchy was NavController -> TableController -> DetailController. After adding method to custom NavBar class add it to the VC you want to RETURN to. You will then be able to ctrl-drag to exit. 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 或 TabBarController(不确定 TabBarController)进行展示,则需要继承导航控制器并为其添加展开转场。在情节提要中不要忘记更改导航控制器的类。我的视图层次结构是 NavController -> TableController -> DetailController。将方法添加到自定义 NavBar 类后,将其添加到要返回的 VC。然后您就可以按住 ctrl-drag 退出。我认为这是一个错误,因为一旦它连接起来,我就可以删除子类并返回股票 NavigationController 并且它仍然有效。

回答by Berkan Ercan

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; (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.exit icon right click

添加此功能后,您将能够从故事板上的任何视图控制器看到此展开功能。只需右键单击任何视图控制器顶部的退出图标。右击退出图标

If you want to perform unwind with a button, then you can cntrl + drag from the button to exit icon and choose your unwindSeguefunction. Done!

如果您想使用按钮执行放松,那么您可以在按钮上按住 cntrl + 拖动退出图标并选择您的unwindSegue功能。完毕!

Unwind with Button

用按钮放松

If you want to perform unwind programmatically, then cntrl + drag from the viewController icon to the exit icon and choose the unwind function.

如果你想以编程方式执行unwind,那么从viewController 图标按住cntrl + 拖动到退出图标,然后选择unwind 功能。

enter image description here

在此处输入图片说明

Afterwards, open the Document Outline and click on the unwind segue.

然后,打开文档大纲并单击展开转场。

enter image description here

在此处输入图片说明

Go to Attributes Inspector inside Utilities and give an identifier to your unwind segue.

转到 Utilities 中的 Attributes Inspector 并为您的 unwind segue 提供一个标识符。

enter image description here

在此处输入图片说明

Lastly, call the performSegueWithIdentifierfunction like the following;

最后,调用如下performSegueWithIdentifier函数;

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

Hope that helps!

希望有帮助!

回答by Pei Xiang

Got it working on Xcode 6.2. My case is ViewController A presenting B modally. Unwind setup steps below:

让它在 Xcode 6.2 上工作。我的情况是 ViewController A 以模态方式呈现 B。展开设置步骤如下:

  1. In A, write an 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

  3. Click the little '+' sign to the right and link it back to B, select 'manual'

  4. Don't forget to set identifier of the unwind segue if you need to call it programmatically.

  1. 在A中,写一个IBAction

    -(IBAction)unwindFromB:(UIStoryboardSegue *) unwindSegue 
    {
      ViewControllerB *vc = [unwindSegue sourceViewController];
      // get whatever data you want to pass back from B
    }
    
  2. 在storyboard中,找到B并右击顶部的'Exit'按钮,然后'Presenting Segue'菜单将列出你刚刚在A中创建的unwindFromB

  3. 单击右侧的小“+”号并将其链接回 B,选择“手动”

  4. 如果您需要以编程方式调用它,请不要忘记设置展开转场的标识符。

回答by pkamb

I found that I could not dragfrom the UITableViewCell to the Exitobject on the View Controller on the Storyboard.

我发现我无法从 UITableViewCell拖动ExitStoryboard 上的 View Controller 上的对象。

Solution was to firstadd an appropriate IBAction in code. Once an IBAction exists, the drag target will light up.

解决方案是首先在代码中添加一个合适的 IBAction。一旦存在 IBAction,拖动目标就会亮起。

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

}