简体   繁体   中英

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:

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

And then leave it up to XCode to determine where to look using the suffix.

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). 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*) 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.

find the row with the KEY - Main storyboard file base name.

Your info.plist must have this two rows

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

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 .

So you use it directly, for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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