Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 6 Storyboard Unwind Segue with Swift Not Connecting to Exit

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.

like image 633
skabob11 Avatar asked Jun 04 '14 05:06

skabob11


People also ask

How do you unwind segue in Swift?

In your storyboard, create an unwind segue by right-clicking a triggering object and dragging to the Exit control at the top of your view controller's scene.

What is segue in Swift?

Swift version: 5.6. Segues are visual connectors between view controllers in your storyboards, shown as lines between the two controllers. They allow you to present one view controller from another, optionally using adaptive presentation so iPads behave one way while iPhones behave another.


2 Answers

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

like image 160
skabob11 Avatar answered Oct 06 '22 19:10

skabob11


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) {} 
like image 34
trumpeter201 Avatar answered Oct 06 '22 19:10

trumpeter201