根据设备typesselect不同的故事板
我有一个通用的应用程序,我在application:didFinishLaunchingWithOptions
手动加载我的主要故事板application:didFinishLaunchingWithOptions
。
我有2个iPhone和iPad的故事板,有~iPhone
和~iPad
后缀。 我正在加载我的故事板使用:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; self.initialViewController = [storyboard instantiateInitialViewController];
这将Unknown class ViewController in Interface Builder file.
打印Unknown class ViewController in Interface Builder file.
到控制台,显然它不是加载正确的故事板。 但是,当我使用[UIStoryboard storyboardWithName:@"MainStoryboard~iPhone" bundle:nil];
它工作正常,但当然只能用于iPhone。
我错过了什么? 如何使用名称后缀自动select正确的故事板?
我不知道任何基于文件名后缀自动select故事板。 你可以使用userInterfaceIdiom
来selectiPad vs iPhone:
if ([[UIDevice currentDevice] userInterfaceIdiom] ==UIUserInterfaceIdiomPad) { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil]; } else { [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; }
但是,如果您正在使用特定的视图控制器启动它,则只需将“开始”箭头拖到故事板中的首选视图控制器
或者 – select故事板中的视图控制器,转到属性执行器并勾选isInitialViewController
//在appdelegate类中,在启动应用程序时select指定的故事板。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIStoryboard *storyboard1; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { storyboard1 = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:[NSBundle mainBundle]]; } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { storyboard1 = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:[NSBundle mainBundle]]; } UIViewController *vc = [storyboard instantiateInitialViewController]; // Set root view controller and make windows visible self.window.rootViewController = vc; [self.window makeKeyAndVisible]; return YES; }
你可以这样命名你的故事板
- Main.storyboard(iPhone版)
- Main_iPad.storyboard(iPad版)
并select他们这样的
- (UIStoryboard *)deviceStoryboardWithName:(NSString *)name bundle:(NSBundle *)bundle { if (IS_IPAD) { NSString *storyboardIpadName = [NSString stringWithFormat:@"%@_iPad", name]; NSString *path = [[NSBundle mainBundle] pathForResource:storyboardIpadName ofType:@"storyboardc"]; if (path.length > 0) { return [UIStoryboard storyboardWithName:storyboardIpadName bundle:bundle]; } } return [UIStoryboard storyboardWithName:name bundle:bundle]; }
这是您可以直接在您的info.plist文件中设置的另一件事。 无需任何编程工作。 查找名为“主要故事板文件基本名称”的属性,默认情况下该属性中将包含“Main” 。
您可以添加另一个名为“主故事板文件基本名称(iPad)”的属性,然后将其用于iPad。
这就是plist的原始输出:
<key>UIMainStoryboardFile</key> <string>Main</string> <key>UIMainStoryboardFile~ipad</key> <string>iPad</string>
Afaik也可以简单地添加一个名为Main〜iPad.storyboard的故事板(如果UIMainStoryboardFile键被设置为Main)。 这将被拿起来的iPad。 虽然没有经过一段时间的testing。