ios Xcode 6 Storyboard Unwind Segue 与 Swift 未连接到退出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24029586/
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
Xcode 6 Storyboard Unwind Segue with Swift Not Connecting to Exit
提问by skabob11
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.
当尝试将导航栏按钮连接到 Xcode 6 中的 ViewController 的退出项时(不确定是否是 Xcode 6 问题,但值得一提,因为它处于测试阶段)它在自定义类中找不到 Swift 函数。
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:
我在视图上制作了另一个按钮,以确保我可以使用 Swift 获得一个 IBAction 并且我正确地编写了它。这工作正常:
@IBAction func test(sender: AnyObject) {
NSLog("Test")
}
I have seen this questionthat 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.
Xcode 6 处于测试阶段,当然,Swift 非常新,但想看看是否有人在认为它是潜在错误之前遇到过这个问题。
回答by skabob11
This is a known issue with Xcode 6:
这是 Xcode 6 的一个已知问题:
Unwind segue actions declared in Swift classes are not recognized by Interface Builder
Interface Builder 无法识别在 Swift 类中声明的 Unwind segue 操作
In order to get around it you need to:
为了绕过它,您需要:
- Change
class MyViewController
to@objc(MyViewController) class MyViewController
Create an Objective-C header file with a category for MyViewController that redeclares the segue action.
@interface MyViewController (Workaround) - (IBAction)unwindToMyViewController: (UIStoryboardSegue *)segue; @end
In the storyboard, select the instance of MyViewController,?clear its custom class, then set it back to MyViewController.
- 更改
class MyViewController
为@objc(MyViewController) class MyViewController
为 MyViewController 创建一个包含类别的 Objective-C 头文件,以重新声明 segue 操作。
@interface MyViewController (Workaround) - (IBAction)unwindToMyViewController: (UIStoryboardSegue *)segue; @end
在 storyboard 中,选择 MyViewController 的实例,清除其自定义类,然后将其设置回 MyViewController。
After these steps you are able to connect buttons to the exit item again.
在这些步骤之后,您可以再次将按钮连接到退出项。
Xcode 6 Release NotesPDF, Page 10
Xcode 6 发行说明PDF,第 10 页
回答by trumpeter201
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.
现在可以安装的 Xcode 6 Beta 4 支持在 Interface Builder 中连接 unwind segues,而不是使用 Objective-C 解决方法。您现在可以从 iOS 开发中心更新。按住 Control 键单击并从要触发 segue 的 UI 项拖动到退出图标,然后在将以下代码放入目标视图控制器后选择函数 unwindToSegue。
@IBAction func unwindToSegue (segue : UIStoryboardSegue) {}
回答by rajeev
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.
我终于让它工作了;xcode6 IB 现在真的很脆弱(也经常崩溃)。我必须重新启动 IDE,然后才能将导航栏按钮项连接到退出项。我最终重新创建了我的测试项目并按照上述建议(Xcode 6 发行说明 PDF,第 10 页)使其工作。此外,在添加 .h 文件时,我确保选择了我的项目目标,默认情况下未选中。我还通过 Cocoa Touch Class 模板(与空的 swift 文件)创建了我的控制器 swift 存根。我在导航控制器中使用了模态转场。
ListTableViewController.h
ListTableViewController.h
#import <UIKit/UIKit.h>
@interface ListTableViewController
- (IBAction)unwindToList: (UIStoryboardSegue *)segue;
@end
ListTableViewController.swift
ListTableViewController.swift
import UIKit
@objc(ListTableViewController) class ListTableViewController: UITableViewController {
@IBAction func unwindToList(s:UIStoryboardSegue) {
println("hello world");
}
}
hope that helps
希望有帮助
回答by Fred
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.
在可供下载的 Xcode 6 Beta 4 中,支持 unwind segues 和界面构建器。我自己在一个小项目中测试过。
回答by Bob Peterson
In Swift 2.3 I found the external name of the parameter must be "withUnwindSegue":
在 Swift 2.3 中,我发现参数的外部名称必须是“withUnwindSegue”:
@IBAction func unwindToThisView(withUnwindSegue unwindSegue: UIStoryboardSegue) {
...
}
回答by Barlow Tucker
It appears that Xcode 6.1 has fixed this issue. You can now set up unwind segues in Swift with the following code:
Xcode 6.1 似乎已经解决了这个问题。您现在可以使用以下代码在 Swift 中设置 unwind segues:
@IBAction func unwindToList(segue: UIStoryboardSegue) {
// Nothing needed here, maybe a log statement
// print("\(segue)")
}
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.
此方法 - 可以保持为空 - 需要具有 UIStoryboardSegue 类型的方法签名,而不是 AnyObject 或 Interface Builder 将看不到它。
For more detail check the TechNote 2298
有关更多详细信息,请查看技术说明 2298
回答by bkopp
If it's always the same presenting view controller that you'd like to unwind to, you can always just do:
如果它始终是您想要放松的相同呈现视图控制器,您总是可以这样做:
self.navigationController?.popViewControllerAnimated(true)
回答by TMfranken
You may want to verify that the original controller destination that you're trying to unwind to is not embedded inside a Container object. Xcode 6 ain't having that.
您可能想要验证您尝试展开到的原始控制器目标是否未嵌入到 Container 对象中。Xcode 6 没有。
回答by Andreas Meyer
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 :))
我遇到了同样的问题,一开始也是 Xcode Beta 4 ..直到我发现,我只是忘记在相应的控制器中为取消和保存按钮添加@IBOutlet。在此之后,我可以将按钮与退出图标连接 :))
回答by cam_271
Xcode --version 6.4 Swift 1.2
Xcode --version 6.4 Swift 1.2
@IBAction func backButton(sender: AnyObject) { dismissViewControllerAnimated(true, completion: nil) }
@IBAction func backButton(sender: AnyObject) {dismissViewControllerAnimated(true, completion: nil) }