ios performSegueWithIdentifier 没有带标识符的 segue

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

performSegueWithIdentifier has no segue with identifier

iosswiftxcode

提问by Ian

I encountered a crash while testing my app. The following image is my storyboard:

我在测试我的应用程序时遇到了崩溃。下图是我的故事板:

storyboard

故事板

I have the code in View Controller:

我在视图控制器中有代码:

@IBAction func unwindToHomeScreen(segue:UIStoryboardSegue) {

}

The view of "Add new Item" have a "Cancel" button, I controlled-drag it to the "Exit" icon at the top and link to unwindToHomeScreen, it works fine.

“添加新项目”的视图有一个“取消”按钮,我控制拖动它到顶部的“退出”图标并链接到unwindToHomeScreen,它工作正常。

The "Add new Item" has a class AddTableViewControllerand the code I wrote inside:

“添加新项目”有一个类AddTableViewController和我在里面写的代码:

@IBAction func save() {
    // Execute the unwind segue and go back to the home screen
    performSegueWithIdentifier("unwindToHomeScreen", sender: self)
}

I controlled-drag the "Save" button to the func, but the app crash when I click the button

我控制拖动“保存”按钮到 func,但当我点击按钮时应用程序崩溃

I can use dismissViewControllerAnimated(true, completion: nil)instead, but I just wonder why performSegueWithIdentifiercan't work?

我可以使用dismissViewControllerAnimated(true, completion: nil),但我只是想知道为什么performSegueWithIdentifier不能工作?

I read the code at dropbox.com/s/hpybgg9x67rtqng/foodpinstatictable.zip?dl=0and try to make one and using performSegueWithIdentifierlike this example for practicing, I didn't see the segue identifier at her, what is the difference?

我在dropbox.com/s/hpybgg9x67rtqng/foodpinstatictable.zip?dl=0 上阅读了代码并尝试制作一个并使用performSegueWithIdentifier此示例进行练习,我没有在她身上看到 segue 标识符,有什么区别?

回答by AstroCB

You haven't actually given that segue an identifier. unwindToHomeScreenis a function that you can call; what you're looking for is your segue identifier, which is set in Interface Builder like this:

你实际上还没有给那个 segue 一个标识符。unwindToHomeScreen是一个可以调用的函数;您正在寻找的是您的 segue 标识符,它在 Interface Builder 中设置如下:

Set an identifier

设置标识符

In this example, I have a button wired to the next view via Interface Builder like you describe ("Interface Builder") and a button that is wired to this IBActionin my view controller ("Programmatically"):

在这个例子中,我有一个按钮通过 Interface Builder 连接到下一个视图,就像你描述的一样(“Interface Builder”)和一个IBAction在我的视图控制器中连接到这个的按钮(“Programmatically”):

@IBAction func goToNextView(sender: UIButton!) {
    self.performSegueWithIdentifier:("go", sender: self)
}

In any case, what you're missing is the actual identifier, which can be set in the attributes of a segue created in Interface Builder.

无论如何,您缺少的是实际标识符,它可以在 Interface Builder 中创建的 segue 的属性中设置。

回答by Phil Hudson

I found that because I renamed my view controller, I needed to delete and recreate the segue.

我发现因为我重命名了我的视图控制器,所以我需要删除并重新创建 segue。

回答by Kristian

Swift 4:

斯威夫特 4:

Sometimes we must cleanthe build folder and then try again.

有时我们必须清理构建文件夹,然后再试一次。

Worked for me after ctrl-dragging the new segue and giving it a name, then using it programatically as:

在 ctrl 拖动新 segue 并为其命名后为我工作,然后以编程方式使用它:

performSegue(withIdentifier: "goToMyNewViewController" , sender: self)

回答by Stéphane de Luca

A possible issue with iOS?12 and earlier (iOS?13 seems not to suffer from the same issue) might come from the fact that performSegue(withIdentifier:,sender:) is called from the view controller viewdidLoad() callback.

iOS?12 和更早版本(iOS?13 似乎没有遇到同样的问题)可能存在的问题可能来自于从视图控制器 viewdidLoad() 回调调用 performSegue(withIdentifier:,sender:)的事实。

You absolutely must invoke it from the viewDidAppear(_ animated: Bool)callbackin order to avoid the question-mentioned crash.

您绝对必须从viewDidAppear(_animated: Bool)回调调用它,以避免出现问题提到的崩溃。

That will certainly save hours of hair-puling…

这肯定会节省数小时的拔毛时间……

回答by Espacucho

in my case reorder the Embed in

在我的情况下重新排序嵌入

Tab bar controller Navigation controller vc (contains button - tapping button initiates a "show, e.g. push" segue)

标签栏控制器导航控制器 vc(包含按钮 - 点击按钮启动“显示,例如推”segue)

Too fix the title in the navigationBar

太固定导航栏中的标题

回答by Sumit

for me --> click on the relation-arrow between view controllers and then select attribute inspector, there is identifier give name and select push class done...

对我来说 --> 单击视图控制器之间的关系箭头,然后选择属性检查器,有标识符给出名称并选择推送类完成...