本文介绍了什么是 StoryBoard ID,我该如何使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am 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?

推荐答案

storyboard ID 是一个 String 字段,您可以使用它来创建基于该 Storyboard ViewController 的新 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 并将其呈现在您当前的视图控制器之上

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

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

And if you are in your app delegate you could use

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

斯威夫特

@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,我该如何使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 14:23