ios swift 3 prepare(for segue:) 功能坏了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39563828/
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-08-31 10:13:20  来源:igfitidea点击:

swift 3 prepare(for segue: ) function broken?

iosswiftsegueswift3

提问by John Durand

for some odd reason , with swift 3, the prepare(for segue:method refuses to acknowledge the segue identifier. I have the following IBAction's connected to a couple button's on the UI:

出于某种奇怪的原因,使用 swift 3,该prepare(for segue:方法拒绝确认 segue 标识符。我有以下 IBAction 连接到 UI 上的几个按钮:

@IBAction func goToImagesPicker(_ sender: AnyObject) {
    performSegue(withIdentifier: "showImagePicker", sender: sender)

}

@IBAction func goToNamePicker(_ sender: AnyObject) {
    performSegue(withIdentifier: "showNamePicker", sender: sender)
}

However in my prepare(for segue:method, it doesn't recognize the different segue identifier's, I know so because my console doesn't log the messages I assigned to each:

但是,在我的prepare(for segue:方法中,它无法识别不同的 segue 标识符,我知道是因为我的控制台没有记录我分配给每个标识符的消息:

func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {

        if segue.identifier == "showImagePicker" {

            print("This is the Image Picker")

        }

        if segue.identifier == "showNamePicker"  {

            print("This is the Name Picker")

        } 
}

any suggestions? or is this just a bug?

有什么建议?或者这只是一个错误?

回答by dan

Your method isn't getting called at all because you have the wrong signature. It was changed in Xcode 8 beta 6 to:

您的方法根本没有被调用,因为您的签名错误。它在 Xcode 8 beta 6 中更改为:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

Note that the type of senderis Any?instead of AnyObject?. You should have had an error after upgrading Xcode which told you your method wasn't overriding any method from its superclass which should have clued you in before you deleted the override.

请注意,类型senderAny?而不是AnyObject?。您应该在升级 Xcode 后遇到错误,该错误告诉您您的方法没有覆盖其超类中的任何方法,在您删除override.