iOS: Using device modifiers for loading xib files?

You can use a device modifier (i.e., ~ipad) to provide a device-specific key in Info.plist, and to specify a device-specific launch image (Default.png for iPhone, and Default~ipad.png for iPad, for example). Those two things are specifically mentioned in Apple Docs, but they don’t say that this will work for any other kinds of files.

I’ve discovered (quite by accident) that this works for loading .xib files via initWithNibName:bundle:. So for example, I can have MyView.xib and MyView~ipad.xib, and this code:

  • UIRefreshController Ending Animation Issue
  • Memory Leak for .showsPhysics
  • How to update a UI label from a non UI thread in iOS
  • DSYM and binary required to symbolicate? Or just DSYM?
  • Problems with window.history using JQuery/Javascript on Cordova app in IOS9
  • Is there a way to persist application data between application installs and uninstalls in iOS
  • MyViewController *viewController = [[MyViewController alloc] 
                                         initWithNibName:@"MyView" bundle:nil];
    

    … will totally load MyView~ipad.xib on an iPad, and MyView.xib on other devices.

    So, 1) Is this documented somewhere? I sure couldn’t find it any any Apple docs. It’s sure handier than checking UI_USER_INTERFACE_IDIOM() and hardcoding two different nib names everywhere, but I kinda don’t trust it if it isn’t documented.

    And, 2) Does anyone know what version of iOS this started working in? I’ve only tried it in 4.2, and it works there. Device modifiers in general (even for the documented things listed above) are 4.0 minimum.

    4 Solutions Collect From Internet About “iOS: Using device modifiers for loading xib files?”

    Actually, it is explicitly defined in the docs, but as a footnote.

    CocoaNibs

    In the note at the bottom of “Loading NIB files using NSBundle”:

    Note: If you are developing a
    Universal application for iOS, you can
    use the device-specific naming
    conventions to load the correct nib
    file for the underlying device
    automatically. For more information
    about how to name your nib files, see
    β€œiOS Supports Device-Specific
    Resources.”

    Which links to Cocoa Conceptual LoadingResources

    However, yes, this is a 4.0+ only feature.

    I had this same problem. The answer didn’t make sense at first, but the good news is that it’s easy to do! πŸ™‚

    Just name your iPad xibs without any modifier and your iPhone xibs with ~iphone modifier and it’ll select them correctly.

    So, with MyViewController, you’ll have MyViewController.xib for the iPad and MyViewController~iphone.xib for the iPhone. Then you can just init your view controller with simple alloc/init.

    [[MyViewController alloc] init] and it’ll grab the right xib.

    So, when I create a new view controller in XCode, I always choose the box to format it for ipad, because the xib it will create will be named MyViewController.xib and you want that one to be the iPad sized xib. Then I create a second xib, formatted for iPhone and name it with the ~iphone modifier.

    The documentation is a little contradictory at times, but this page talks about how resources with an identifier will default to iPad.

    ImageSoundResources

    Check the section about using high res images. I know we’re talking xibs and not images, but it does work. My last 6 apps have all used this idiom.

    I hate to be that guy and answer my own question, but I think the answer is:

    1) Nope, not explicitly documented in any Apple documentation, and
    2) 4.0 and higher (this based on my own testing)

    All you really save is a couple lines of code checking for UI_USER_INTERACE_IDIOM(). Still, I’ll take it. Less code is less code.

    The appropriate technique to use in iOS 3.2 and later is the UI_USER_INTERFACE_IDIOM() function. I typically use a ternary operator to init the UIViewController with the appropriate XIB.

    UIViewController* controller = [[UIViewController alloc] 
        initWithNibName:UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? 
        @"YourViewController-iPad" : @"YourViewController" andBundle:nil];