ios Xcode,在哪里分配 segue 标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39904328/
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, where to assign the segue identifier
提问by Taxi Noi Bai Ha Noi
Pardon me for beginner's question. I know I can switch to another screen (ViewController) like this
请原谅我初学者的问题。我知道我可以像这样切换到另一个屏幕(ViewController)
self.performSegueWithIdentifier ("SecondViewController", sender: self)
but I can't seem to find where to assign my 2nd screen the id, I just find Storyboard ID, is that it?
但我似乎找不到在哪里为我的第二个屏幕分配 id,我只找到 Storyboard ID,是吗?
I've already tried, only received a crash with the following error:
我已经尝试过,只收到以下错误的崩溃:
Receiver () has no segue with identifier 'SecondViewController'
Receiver () 没有标识符为“SecondViewController”的segue
Any idea? thanks
任何的想法?谢谢
回答by Ahmad F
Segue Identifieris notthe 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-.
Segue公司标识是不一样的故事板ID,当你想基于特定故事创造一个视图控制器使用故事板ID -和它必须是唯一的,不像SEGUE identifier-。
If you already know how to create a segue, you can skip this part.
如果您已经知道如何创建转场,则可以跳过这一部分。
Adding a segue between two viewControllers:
在两个 viewController 之间添加一个 segue:
From the Interface Builder, press the ctrland 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:
在 Interface Builder 中,按ctrl并在要链接的两个视图控制器之间拖动(确保从视图控制器本身而不是其主视图拖动)。你应该看到:
Choose the "Show" -for instance-, the output should look like this:
选择“显示”-例如-,输出应如下所示:
As shown above, the arrow that surrounded by the red rectangle is the segue.
如上图,被红色矩形包围的箭头就是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.
因为“显示”意味着推入导航控制器堆栈。
Assigning an identifier for the segue:
为 segue 分配一个标识符:
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
.
选择 segue,从属性检查器中您将看到“标识符”文本字段,就是这样!确保插入与performSegueWithIdentifier
.
If you don't know where to find the attribute inspector, it is on the top right looks like:
如果你不知道在哪里可以找到属性检查器,它在右上角看起来像:
Furthermore:
此外:
For adding multiplesegues from one View Controller, follow the same process (ctrl+ drag from the first controller to each other View Controller), the output should looks like:
要从一个 View Controller添加多个segue,请遵循相同的过程(ctrl+ 从第一个控制器拖动到另一个 View Controller),输出应如下所示:
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:
在这种情况下,您可能会面临如何识别执行了哪个 segue 的问题,覆盖prepare(for:sender:)
方法是解决方案,您可以根据 segueidentifier
属性进行检查:
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.
这将是您添加到故事板中的转场的名称。
回答by Harish Singh
In your code
在你的代码中
self.performSegueWithIdentifier ("SecondViewController", sender: self)
the string "SecondViewController" is looking like storyboard id . At the same place you have to write the segue identifier , not storyboard id .
字符串 "SecondViewController" 看起来像 storyboard id 。在同一地方,您必须编写 segue 标识符,而不是 storyboard id。
Follow the Screenshot and assign segue identifier name by clicking on the segue on right top bar field. you can do like this
按照屏幕截图并通过单击右上角栏字段上的 segue 分配 segue 标识符名称。你可以这样做
self.performSegueWithIdentifier ("WriteSegueIdentifierName", sender: self)
回答by Abuzar Manzoor
You can initiate viewController like this:
您可以像这样启动 viewController:
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
self.present(viewController, animated: false, completion: nil)
回答by Bhavuk Jain
When you link a View controller to another View controller in the storyboard, in the link between them you need to assign a segue identifier i.e "SecondViewController" only then your code will work.
当您将一个视图控制器链接到故事板中的另一个视图控制器时,在它们之间的链接中,您需要分配一个 segue 标识符,即“SecondViewController”,只有这样您的代码才能工作。
Alternatively, you can also show another view controller through storyboard id using self.storyboard.instantiateViewControllerWithIdentifier("//storyboard id of that view controller") and then either use present/show view controller.
或者,您也可以使用 self.storyboard.instantiateViewControllerWithIdentifier("//storyboard id of that view controller") 通过 storyboard id 显示另一个视图控制器,然后使用present/show view controller。