xcode UIStoryboard 名称和包

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

UIStoryboard name and bundle

iosxcode

提问by Alessandro

I have this line of code:

我有这行代码:

[UIStoryboard storyboardWithName:(NSString *) bundle (NSBundle *)];

and a storyboard file. Where do I actually insert the name of the controller, and where do I find the bundle?

和故事板文件。我实际上在哪里插入控制器的名称,在哪里可以找到包?

It is a stupid question, but I can't figure it out

这是一个愚蠢的问题,但我无法弄清楚

回答by Anatoliy Gatt

Everything is really straightforward!

一切都非常简单!

This is the code:

这是代码:

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

Where is @"Main Storyboard"is the name of your .storyboardfile. Bundle is just the bundle that contains your .storyboardfile, usually is the main bundle of your app.

@"Main Storyboard"在哪里是您的.storyboard文件的名称。Bundle 只是包含您的.storyboard文件的包,通常是您的应用程序的主包。

If you want to get access to some of the UIViewControllers, for example, with the purpose of pushing it to the stack of your UINavigationController, then you must do this:

如果你想访问一些 UIViewControllers,例如,为了将它推送到你的 UINavigationController 的堆栈,那么你必须这样做:

UIViewController *yourViewController = 
[storyboard instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"];

You can set Identifier to your UIViewController in the Xcode's Identity Inspector!

您可以在 Xcode 的 Identity Inspector 中将 Identifier 设置为您的 UIViewController!

Hope it helps you!

希望对你有帮助!

回答by AliSoftware

The nameparameter is the name of your .storyboardfile(without the extension).

name参数是你的名字.storyboard的文件(不带扩展名)。

The bundleis usually the main bundle of your application. In general, you pass nilso that the runtime will search your storyboard file in the main bundle by default (same as if you pass [NSBundle mainBundle]). And in practice you generally only have one bundle in iOS applications, which is the main bundle representing your IPA bundle, so nilis the common value for this parameter)

bundle通常是应用程序的主束。通常,您通过,nil以便运行时默认在主包中搜索您的故事板文件(与通过时相同[NSBundle mainBundle])。在实践中你通常在 iOS 应用程序中只有一个包,它是代表你的 IPA 包的主包,所以nil是这个参数的通用值)

For example, if you have a storyboard named MyStory.storyboardyour would write:

例如,如果你有一个名为MyStory.storyboardyour的故事板,你会写:

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


Actually all of this is explained in the Apple documentation for this method.

实际上,所有这些都在 Apple 文档中对此方法进行了解释。

storyboardWithName:bundle:

Creates and returns a storyboard object for the specified storyboard resource file.

+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil

Parameters

  • name: The name of the storyboard resource file without the filename extension. This method raises an exception if this parameter is nil.
  • storyboardBundleOrNil: The bundle containing the storyboard file and its related resources. If you specify nil, this method looks in the main bundle of the current application.

storyboardWithName:bundle:

为指定的故事板资源文件创建并返回一个故事板对象。

+ (UIStoryboard *)storyboardWithName:(NSString *)name bundle:(NSBundle *)storyboardBundleOrNil

参数

  • name没有文件扩展名的故事板资源文件的名称。如果此参数为零,则此方法会引发异常。
  • storyboardBundleOrNil:包含故事板文件及其相关资源的包。如果指定 nil,则此方法会在当前应用程序的主包中查找。

回答by Jaswanth Kumar

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

回答by Michael

Swift 5 Version:

斯威夫特 5 版本:

UIStoryboard(name: "Main", bundle: nil)

In this example, I used the default "Main" storyboard file as the reference.

在这个例子中,我使用默认的“主”故事板文件作为参考。