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

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

问题描述

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

Pardon me for beginner's question in Swift 2. 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 (<MyApps.RegViewController: 0x7fda5ae3abf0>) has no segue with identifier 'SecondViewController''

有什么想法吗?谢谢

推荐答案

Segue Identifier 相同故事板ID ,当您想要基于特定故事板创建视图控制器时使用的故事板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 have to be unique, unlike the segue identifier-.

如果你已经知道如何创建一个segue,你可以跳过这一部分。

在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.

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

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,从属性检查器中你会看到Identifier文本字段,就是这样!确保插入与 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.

如果你不知道在哪里可以找到属性检查器,它位于右上方,如下所示:

添加<来自一个View Controller的em>多个 segue,按照相同的过程( ctrl +从第一个控制器拖动到另一个View Controller),输出应如下所示:

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:

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

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") {
        //...
    }
}

这是你在故事板中添加到segue的名称。

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

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

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