Xcode,在哪里分配 segue 标识符 [英] Xcode, where to assign the segue identifier

查看:20
本文介绍了Xcode,在哪里分配 segue 标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅初学者的问题.我知道我可以像这样切换到另一个屏幕(ViewController)

Pardon me for beginner's question. I know I can switch to another screen (ViewController) like this

self.performSegueWithIdentifier ("SecondViewController", sender: self)

但我似乎找不到在哪里为我的第二个屏幕分配 ID,我只找到 Storyboard ID,是吗?

but I can't seem to find where to assign my 2nd screen the id, I just find Storyboard ID, is that it?

我已经试过了,只收到了以下错误的崩溃:

I've already tried, only received a crash with the following error:

Receiver() 没有 segue带有标识符SecondViewController"

Receiver () has no segue with identifier 'SecondViewController'

有什么想法吗?谢谢

推荐答案

Segue Identifier notstoryboard ID 一样,storyboard ID当您想基于该特定故事板创建视图控制器时使用 - 它必须是唯一的,与 segue 标识符不同 -.

Segue Identifier is not the same as storyboard ID, storyboard ID used when you want to create a View Controller based on that specific storyboard -and it has to be unique, unlike the segue identifier-.

如果您已经知道如何创建转场,则可以跳过这一部分.

在 Interface Builder 中,按下 ctrl 并在要链接的两个视图控制器之间拖动(确保从视图控制器本身拖动,而不是从其主视图拖动).你应该看到:

From the Interface Builder, press the ctrl and drag between the two View Controllers that you want to link (make sure that you are dragging from the view controller itself, not the its main view). You should see:

选择显示"-例如-,输出应如下所示:

Choose the "Show" -for instance-, the output should look like this:

如上图,被红色矩形包围的箭头就是segue.

As shown above, the arrow that surrounded by the red rectangle is the segue.

附加说明:如果您选择了显示"选项,则必须将您的第一个视图控制器嵌入到导航控制器中(选择您的第一个视图控制器 -> 编辑器 -> 嵌入 -> 导航控制器),输出应如下所示:

Additional note: if you selected the "Show" option, you have to embed your first view Controller in a Navigation Controller (select your first viewController -> Editor -> Embed In -> Navigation Controller), the output should looks like:

因为显示"意味着推入导航控制器堆栈.

Because the "Show" means pushing into a navigation controller stack.

选择segue,从属性检查器中您将看到标识符"文本字段,就是这样!确保插入与 performSegueWithIdentifier 中使用的名称完全相同的名称.

Select the segue, from the attribute inspector you'll see "Identifier" text field, that's it! make sure to insert the exact same name that used in performSegueWithIdentifier.

如果你不知道在哪里可以找到属性检查器,它在右上角看起来像:

要从一个视图控制器添加多个 segues,请遵循相同的过程(ctrl + 从第一个控制器拖动到另一个视图控制器),输出应如下所示:

For adding multiple segues from one View Controller, follow the same process (ctrl + drag from the first controller to each other View Controller), the output should looks like:

在这种情况下,您可能会面临如何识别执行了哪个转场的问题,覆盖了 prepare(for:sender:) 方法是解决方案,您可以根据 segue identifier 属性进行检查:

In this case, you might face the issue how to recognize which segue has been performed, overriding prepare(for:sender:) method is the solution, you can make the checking based on the segue identifier property:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "firstSegueIdentifier") {
        // ...
    } else if (segue.identifier == "secondSegueIdentifier") {
        //...
    }
}

这将是您添加到故事板中的转场的名称.

which would be the name that you have added to the segue in the storyboard.

这篇关于Xcode,在哪里分配 segue 标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆