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.
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.
I had the same problem, also with Xcode Beta 4 at the beginning.. till I found out, that I simply forgot to add the @IBOutlet for the Cancel and Save Buttons in the respective controller. After this, I could connect the buttons with the Exit-Icon :))
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.
This is a known issue with Xcode 6:
In order to get around it you need to:
class MyViewController
to@objc(MyViewController) class MyViewController
Create an Objective-C header file with a category for MyViewController that redeclares the segue action.
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
It appears that Xcode 6.1 has fixed this issue. You can now set up unwind segues in Swift with the following code:
This method - which can remain empty - needs to have a method signature with the UIStoryboardSegue type and not AnyObject or Interface Builder will not see it.
For more detail check the TechNote 2298
The answers above rely on ObjC to fix the issue, I have found a pure Swift solution. While adding the segue handler in Swift allowed me to create the unwind segue in Interface Builder (Xcode 6.3), the handler was not being called.
So after digging in, the
canPerformUnwindSegueAction:fromViewController:withSender
from the super class returnsfalse
. So I've overridden the implementation, and it works:Update
The code above is incorrect, as I resolved the issue without overriding
canPerformUnwindSegueAction:fromViewController:withSender
. The fundamental error was to make the distinction between the presenting viewcontroller and the presented viewcontroller.So, define the
@IBAction
on the presenting viewcontroller, not on the presented view controller. That way the segue will have meaningful values for the propertiesdestinationViewController
andsourceViewController
as well, being respectively the presenting and presented viewcontroller.In Swift 2.3 I found the external name of the parameter must be "withUnwindSegue":