问题描述
我是IOS开发的新手,最近刚开始使用Xcode 4.5。我看到每个viewController都可以设置一些身份变量,包括故事板ID。这是什么以及如何使用它?
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?
我开始在stackoverflow上搜索,但找不到任何解释。
我认为这不仅仅是一些愚蠢的标签,我可以设置为记住我的控制器吗?它是做什么的?
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?
推荐答案
故事板ID是一个String字段,可用于根据该故事板创建新的ViewController视图控制器。一个示例用法来自任何ViewController:
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:
//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];
}
这将基于您命名为MyViewController的故事板ViewController创建一个MyCustomViewController,将它显示在当前的View Controller上方
This will create a MyCustomViewController based on the storyboard ViewController you named "MyViewController" and present it above your current View Controller
如果你在你的app代理中,你可以使用
And if you are in your app delegate you could use
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];
编辑:Swift
@IBAction func buttonPressed(sender: AnyObject) {
let vc = storyboard?.instantiateViewControllerWithIdentifier("MyViewController") as MyCustomViewController
presentViewController(vc, animated: true, completion: nil)
}
编辑Swift> = 3:
Edit for Swift >= 3:
@IBAction func buttonPressed(sender: Any) {
let vc = storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! ViewController
present(vc, animated: true, completion: nil)
}
和
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
这篇关于什么是StoryBoard ID,我该如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!