我是 IOS 开发的新手,最近开始使用 Xcode 4.5。我看到我可以为每个 viewController 设置一些身份变量,包括 Storyboard ID。这是什么,我该如何使用它?
我开始在 stackoverflow 上搜索,但找不到任何解释。
我认为这不仅仅是一些愚蠢的标签,我可以设置它来记住我的 Controller ,对吗?它有什么作用?
最佳答案
Storyboard ID 是一个字符串字段,您可以使用它来基于该 Storyboard 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];
}
这将基于您命名为“MyViewController”的 Storyboard ViewController 创建一个 MyCustomViewController,并将其显示在您当前的 View Controller 之上
如果你在你的 app delegate 中,你可以使用
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 编辑:
@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)
关于xcode - 什么是 StoryBoard ID,我该如何使用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13867565/