ios UIStoryboard:获取活动故事板的正确方法是什么?

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

UIStoryboard: What's the Correct Way to Get the Active Storyboard?

iosruntimestoryboard

提问by Chris Marshall

I am currently furiously digging through all the docs, and haven't quite found what I'm looking for. I suspect it is a real d'oh! answer.

我目前正在疯狂地翻阅所有文档,但还没有完全找到我要找的东西。我怀疑这是一个真正的 d'oh!回答。

I simply need to find the active storyboard in the main bundle, and want to know the best way to do this.

我只需要在主包中找到活动的故事板,并想知道这样做的最佳方法。

This is so that I can use the [UIStoryboard storyboardWithName:@"XXX" bundle:mainBundle]to extract the running storyboard.

这样我就可以使用[UIStoryboard storyboardWithName:@"XXX" bundle:mainBundle]来提取正在运行的故事板。

I know how to kludge it by switching on the idiom, but I feel that this is a...kludge.

我知道如何通过打开成语来混杂它,但我觉得这是一个......混杂。

What's a correct way of doing this?

这样做的正确方法是什么?

采纳答案by Chris Marshall

OK. As my comment above indicates, I found the answer to the (badly phrased question):

好的。正如我上面的评论所示,我找到了(措辞不当的问题)的答案:

I wanted to be able to get the main(not active) storyboard, as I'm not using multiple storyboards per incarnation. I'm using the standard model of 1 storyboard for iPhone, and 1 for iPad. I just wanted the cleanest way to get the storyboard, so that I could use it to generate a view controller.

我希望能够获得主要(非活动)故事板,因为我没有在每个化身中使用多个故事板。我正在使用 iPhone 的 1 个故事板和 iPad 的 1 个故事板的标准模型。我只想以最简洁的方式获取故事板,以便我可以使用它来生成视图控制器。

I found the answer in this post on Stack Overflow, and implemented it with the following code:

在 Stack Overflow 上的这篇文章中找到了答案,并使用以下代码实现了它:

UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];

回答by olivaresF

In case you want to get the active storyboard for a viewController, there's a storyboard property. This is how I solved it, instead of making a new instance:

如果您想获取 viewController 的活动情节提要,则有一个情节提要属性。这就是我解决它的方法,而不是创建一个新实例:

LoginViewController *vc = [navController.storyboard instantiateViewControllerWithIdentifier:@"firstLaunch"];
[navController presentModalViewController:vc animated:YES];

In Swift you'd call:

在 Swift 中,您会调用:

let loginViewController = navigationController?.storyboard?.instantiateViewController(withIdentifier: "firstLaunch") as! LoginViewController
navigationController?.present(loginViewController, animated: true, completion: nil)

You could also be a lot safer by using guards against the navigation controller and the storyboard. I've used as!so as to guarantee that you're getting a LoginController.

通过使用对导航控制器和故事板的防护,您也可以更安全。我已经使用as!以便保证您获得 LoginController。

回答by nburk

In Swift, you'd use the following syntax:

Swift 中,您将使用以下语法:

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

Note that passing nilto bundlewill make the call refer to your mainbundle automatically.

请注意,传递nilbundle将使调用自动引用您的包。

If you're in a view controllerthat you have on the Storyboard and want to instantiate the Storyboard from there directly, you can just do:

如果您在Storyboard 上的视图控制器中并想直接从那里实例化 Storyboard,您可以这样做:

let storyboard: UIStoryboard? = self.storyboard // call this inside a VC that is on the Storyboard

Note that in the last case, self.storyboardwill return an optionalStoryboard (Storyboard?), so if you'd like to use it unwrap it like so:

请注意,在最后一种情况下,self.storyboard将返回一个可选的Storyboard ( Storyboard?),因此如果您想使用它,请像这样解开它:

if let storyboard = self.storyboard {
  // access storyboard here
}

回答by Imran Khan

I have just copy pasted the code form above updated question so that everyone can see it as an answer.

我刚刚复制粘贴了更新问题上方的代码表单,以便每个人都可以将其视为答案。

UIStoryboard *st = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];