xcode 什么是 StoryBoard ID,我该如何使用它?

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

What is a StoryBoard ID and how can i use this?

xcodestoryboardinterface-builder

提问by RTB

i'm new to IOS developing and recently started in Xcode 4.5. I saw for every viewController that i could set some identity variables including the storyboard ID. What is this and how can i use it?

我是 IOS 开发的新手,最近开始使用 Xcode 4.5。我看到每个 viewController 都可以设置一些身份变量,包括故事板 ID。这是什么以及如何使用它?

enter image description here

在此处输入图片说明

I started searching on stackoverflow and couldn't find any explanation for it. I assumed it's not just some stupid label that i can set to remember my controller right? What does it do?

我开始在 stackoverflow 上搜索,但找不到任何解释。我认为这不仅仅是一些愚蠢的标签,我可以设置它来记住我的控制器,对吗?它有什么作用?

回答by Eric

The storyboard ID is a String field that you can use to create a new ViewController based on that storyboard ViewController. An example use would be from any ViewController:

故事板 ID 是一个字符串字段,可用于基于该故事板 ViewController 创建新的 ViewController。一个示例使用将来自任何 ViewController:

//Maybe make a button that when clicked calls this method

- (IBAction)buttonPressed:(id)sender
{
    MyCustomViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

   [self presentViewController:vc animated:YES completion:nil];
}

This will create a MyCustomViewController based on the storyboard ViewController you named "MyViewController" and present it above your current View Controller

这将基于您命名为“MyViewController”的故事板 ViewController 创建一个 MyCustomViewController 并将其呈现在您当前的视图控制器之上

And if you are in your app delegate you could use

如果你在你的应用程序委托中,你可以使用

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];

Edit: Swift

编辑:斯威夫特

@IBAction func buttonPressed(sender: AnyObject) {
    let vc = storyboard?.instantiateViewControllerWithIdentifier("MyViewController") as MyCustomViewController
    presentViewController(vc, animated: true, completion: nil)
}

Edit for Swift >= 3:

为 Swift >= 3 进行编辑:

@IBAction func buttonPressed(sender: Any) {
    let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController
    present(vc, animated: true, completion: nil)
}

and

let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)

回答by Taiwosam

To add to Eric's answer and update it for Xcode 8 and Swift 3:

添加到 Eric 的答案并为 Xcode 8 和 Swift 3 更新它:

A storyboard ID does exactly what the name implies: it identifies. Just that it identifiesa view controllerin a storyboard file. It is how the storyboard knows which view controller is which.

故事板 ID 的作用正如其名称所暗示的那样:它用于标识。只是它标识了故事板文件中的视图控制器。这是故事板如何知道哪个视图控制器是哪个。

Now, don't be confused by the name. A storyboard ID doesn't identify a 'storyboard'. A storyboard, according to Apple's documentation, 'represents the view controllers for all or part of your app's user interface.' So, when you have something like the picture below, you have a storyboard called Main.storyboard which has two view controllers, each of which could be given a storyboard ID (their ID in the storyboard).

现在,不要被这个名字所迷惑。故事板 ID 不标识“故事板”。根据 Apple 的文档,故事板“代表了应用程序用户界面的全部或部分的视图控制器。” 所以,当你有类似下图的东西时,你有一个名为 Main.storyboard 的故事板,它有两个视图控制器,每个视图控制器都可以被赋予一个故事板 ID(它们在故事板中的 ID)。

enter image description here

在此处输入图片说明

You can use a view controller's storyboard ID to instantiate and return that view controller. You can then go ahead to manipulate and present it however you want. To use Eric's example, say you want to present a view controller with identifier 'MyViewController' when a button is pressed, you would do it this way:

您可以使用视图控制器的故事板 ID 来实例化并返回该视图控制器。然后,您可以根据需要继续操作和呈现它。以 Eric 的例子为例,假设你想在按下按钮时显示一个带有标识符“MyViewController”的视图控制器,你可以这样做:

@IBAction func buttonPressed(sender: Any) {
    // Here is where we create an instance of our view controller. instantiateViewController(withIdentifier:) will create an instance of the view controller every time it is called. That means you could create another instance when another button is pressed, for example.
    let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController
    present(vc, animated: true, completion: nil)
}

Please take note of changes in syntax.

请注意语法的变化。