Xcode 6 Storyboard Unwind Segue with Swift Not Connecting to Exit

42,850

Solution 1

This is a known issue with Xcode 6:

Unwind segue actions declared in Swift classes are not recognized by Interface Builder

In order to get around it you need to:

  1. Change class MyViewController to @objc(MyViewController) class MyViewController
  2. Create an Objective-C header file with a category for MyViewController that redeclares the segue action.

    @interface MyViewController (Workaround)
    - (IBAction)unwindToMyViewController: (UIStoryboardSegue *)segue;
    @end
    
  3. In the storyboard, select the instance of MyViewController, clear its custom class, then set it back to MyViewController.

After these steps you are able to connect buttons to the exit item again.

Xcode 6 Release Notes PDF, Page 10

Solution 2

Instead of using the Objective-C workaround, Xcode 6 Beta 4, which can now be installed, supports the connection of unwind segues in the Interface Builder. You can update now from the iOS Dev center. Control-click and drag from the UI item you want to trigger the segue to the exit icon, and select the function unwindToSegue after having put the following code in the destination view controller.

@IBAction func unwindToSegue (segue : UIStoryboardSegue) {}

Solution 3

I was able to finally get it to work; the xcode6 IB is really fragile right now (crashes a lot too). I had to restart the IDE before I could connect the nav bar button item to the exit item. I ended up re-creating my test project and following the above suggestion (Xcode 6 Release Notes PDF, Page 10) to get it to work. In addition, when adding the .h file, I made sure to select my project target, which was unchecked by default. I also created my controller swift stub via the Cocoa Touch Class template (vs empty swift file). I used a modal segue in my nav controller.

ListTableViewController.h

#import <UIKit/UIKit.h>

@interface ListTableViewController
- (IBAction)unwindToList: (UIStoryboardSegue *)segue;
@end

ListTableViewController.swift

import UIKit

@objc(ListTableViewController) class ListTableViewController: UITableViewController {

    @IBAction func unwindToList(s:UIStoryboardSegue) {
        println("hello world");
    }

}

hope that helps

Solution 4

In Xcode 6 Beta 4 which is available for download, unwind segues and interface builder is supported. I have tested it by myself in a little project.

Solution 5

In Swift 2.3 I found the external name of the parameter must be "withUnwindSegue":

@IBAction func unwindToThisView(withUnwindSegue unwindSegue: UIStoryboardSegue) {
    ...
}
Share:
42,850
skabob11
Author by

skabob11

Interactive Design Director at Maark in Boston. Working on my own Mac Desktop applications in my free time.

Updated on July 22, 2022

Comments

  • skabob11
    skabob11 almost 2 years

    When trying to connect a Navigation Bar Button to the Exit item of a ViewController in Xcode 6 (not really sure if it's an Xcode 6 problem but worth mentioning as it is in beta) it does not find the Swift function in the custom class.

    Button to Exit with whip

    The function it should be finding:

    @IBAction func unwindToList(segue: UIStoryboardSegue) {
    
    }
    

    I made another button on the view just to make sure I could get an IBAction working with Swift and that I was writing it correctly. This works fine:

    @IBAction func test(sender: AnyObject) {
    
        NSLog("Test")
    }
    

    I have seen this question that seems like the same issue but according to the answers there this should be working.

    Xcode 6 is in beta and, of course, Swift is very new, but wanted to see if anyone has come across this before considering it a potential bug.

  • itsmequinn
    itsmequinn almost 10 years
    You're right that the documentation does say this is the workaround for the issue, but I can't seem to get it to work. I've added the header file and edited the swift class file (and reset the custom class for the view controller) but nothing seems to work. Is there anything I might be missing that you can think of?
  • ehfeng
    ehfeng almost 10 years
    @itsmequinn I had the same problem you did. See rajeev's answer below — worked for me.
  • aseba
    aseba almost 10 years
    That Xcode Release Notes link is not valid anymore. Anyone knows where it is now?
  • skabob11
    skabob11 almost 10 years
    @aseba Updated release notes link.
  • elprl
    elprl almost 10 years
    Does anyone know if this was fixed is today's beta 4 release?
  • A Bit of Help
    A Bit of Help almost 10 years
    Hi. I created a little test app with Xcode 6, Beta 4 and the unwind works. However, the reason why I created this app is because it didn't work in another Swift app that I am creating. The unwind setup in both apps is identical, so I cannot explain what is wrong... I am still debugging it and will report back what I've learned.
  • Laurent
    Laurent almost 10 years
    One thing that is not clear in the workaround is that the unwind segue function must be in the target view of the unwind segue. i.e. it must be in the view TO WHICH you unwind, not the view FROM WHICH you unwind. That threw me off but once I fixed that it worked.
  • trumpeter201
    trumpeter201 almost 10 years
    I've tried it both ways... How is your unwind segue set up?
  • trumpeter201
    trumpeter201 almost 10 years
    @IBAction func unwindToSegue (segue:UIUnwindSegue) {}
  • Haider Ghaleb
    Haider Ghaleb almost 10 years
    @IBAction func unwindToSegue(segue: UIStoryboardSegue) {} UIUnwindSegue is not available
  • trumpeter201
    trumpeter201 almost 10 years
    Sorry, yeah, that's what I meant, and what I've been using.
  • Ryan Thom
    Ryan Thom over 9 years
    If this still doesn't work, try to make sure the Module is set correctly as mentioned in this post: stackoverflow.com/questions/24924966/….
  • swilliams
    swilliams over 9 years
    This is fixed in Xcode 6 GM. Dunno if it was working in prior betas.
  • Unome
    Unome over 9 years
    Worked for me as well, what had been missing in the other helps was the fact you need to still declare the @IBAction func unwindToList in your Swift file as well as the .h
  • thsorens
    thsorens over 9 years
    Thank you very much for this, struggled like crazy to get the unwind to work. The objc header way didnt work at all for me.
  • vim
    vim over 9 years
    Yes, but it doesn't seem to really connect, i.e. it does not being called
  • Barlow Tucker
    Barlow Tucker over 9 years
    @vim Huh, that's odd. It seems to be working for me. Did you remember to connect to the exit segue in IB?
  • Sean
    Sean over 9 years
    I'm in XCode 6.2 beta and still need one of these solutions.
  • vim
    vim over 9 years
    @Laurent Gorse saved me after long long time trying to crack this
  • Gaston Sanchez
    Gaston Sanchez over 9 years
    @LaurentGorse thanks, without you I wouldn't have been able to solve it.
  • Roshambo
    Roshambo about 9 years
    @LaurentGorse I was completely stumped until you chimed in. Thank you!
  • Jason Storey
    Jason Storey about 9 years
    Im on Beta 4 and have the outlet as well and it still wont unwind my segue thats presented modally. Any ideas?
  • Nicolas Manzini
    Nicolas Manzini over 8 years
    its a pain in the ass
  • Cameron Lowell Palmer
    Cameron Lowell Palmer over 7 years
    Yes, it is correct to place the unwind in the presenting view controller. Also you don't need the dismissViewControllerAnimated shown above. By virtue of having the unwind segue it handles it automagically.
  • André Pinto
    André Pinto over 7 years
    The Interface Builder in Xcode 8 seems to use Swift 3 syntax (even for 2.3 projects), so if you had unwindToThisView(unwindSegue:) it expects unwindToThisViewWithUnwindSegue: and not unwindToThisView:. I solved it by renaming the method to unwindToThisView(_ unwindSegue:)