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
UIStoryboard name and bundle
提问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 name
parameter is the name of your .storyboard
file(without the extension).
该name
参数是你的名字.storyboard
的文件(不带扩展名)。
The bundle
is usually the main bundle of your application. In general, you pass nil
so 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 nil
is the common value for this parameter)
在bundle
通常是应用程序的主束。通常,您通过,nil
以便运行时默认在主包中搜索您的故事板文件(与通过时相同[NSBundle mainBundle]
)。在实践中你通常在 iOS 应用程序中只有一个包,它是代表你的 IPA 包的主包,所以nil
是这个参数的通用值)
For example, if you have a storyboard named MyStory.storyboard
your would write:
例如,如果你有一个名为MyStory.storyboard
your的故事板,你会写:
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.
在这个例子中,我使用默认的“主”故事板文件作为参考。