简体   繁体   English

切换StoryBoards(iphone / iPad)没有代码?

[英]Switching StoryBoards (iphone/iPad) without code?

Currently I do it this way 目前我是这样做的

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
     ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)?
                                @"Main_iPad" : @"Main_iPhone" bundle:nil];

But I would love to do it just like a UIImage: 但是我喜欢像UIImage那样做:

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

And then leave it up to XCode to determine where to look using the suffix. 然后将其留给XCode以确定使用后缀的位置。

Is that possible? 那可能吗? if so how? 如果是这样的话? if not why ? 如果不是为什么?

Those _iPad and _iPhone suffixes are simple identifiers that can be changed in anything you like when you create a new storyboard and they're not related to the @2x stuff (or Image assets as well). 那些_iPad和_iPhone后缀是简单的标识符,可以在创建新故事板时以任何您喜欢的方式进行更改,并且它们与@ 2x内容(或图像资源)无关。 However, if you make large use of different storyboard (to keep their editing simpler and smoother and their version control merge safer), you can write a category on UIStoryboard and write your own logic in one place with a method like 但是,如果您大量使用不同的故事板(为了使编辑更简单,更顺畅,并且版本控制合并更安全),您可以在UIStoryboard上编写一个类别,并使用类似的方法在一个地方编写您自己的逻辑

+ (UIStoryboard*) appropriateStoryboardWithName:(NSString*) name bundle:(NSBundle*) bundle             
{
    name = [name stringByAppendingString:
    ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) ? @"_iPad" : @"_iPhone"];
    return [UIStoryboard storyboardWithName:name bundle:bundle];
} 

if you want that x-code decides which storyboard load you need to open your info.plist and specify which storyboard should use for iPad and iphone. 如果你想让x-code决定打开你的info.plist需要哪个故事板加载,并指定哪个故事板应该用于iPad和iphone。

find the row with the KEY - Main storyboard file base name. 找到具有KEY - 主故事板文件基本名称的行。

Your info.plist must have this two rows 您的info.plist必须有这两行

KEY                                       TYPE       VALUE

Main storyboard file base name (iPad)    String     Main_iPad
Main storyboard file base name (iPhone)  String     Main_iPhone

Now every time your app starts x-code will decide which storyboard load without code 现在每次你的应用程序启动x-code将决定哪个故事板加载没有代码

Hope it helps 希望能帮助到你

Good Luck!! 祝好运!!

Instead of writing all that code: 而不是编写所有代码:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
     ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)?
                                @"Main_iPad" : @"Main_iPhone" bundle:nil];

Xcode can load the right storyboard if you call self.storyboard . 如果你调用self.storyboard Xcode可以加载正确的故事板。

So you use it directly, for example: 所以你直接使用它,例如:

someViewController *pieVC = [self.storyboard 
                             instantiateViewControllerWithIdentifier:@"someView"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM