Question

I have been trying to figure out why this has been happening but it seems that, in the iPad version of my universal app, it is loading the iPhone .xib instead of the iPad one.

I have named my iPhone xibs with a suffix of ~iphone.xib and I left my iPad ones just with .xib . I read to do that because someone said that worked for them but in my case it did not work for me!

Even if I do ~ipad.xib and ~iphone.xib for the different .xib files, it still loads the iPhone version!

**Is there any way to completely confirm that it is loading the iPhone version instead of the iPad version?

And is there any way to fix this issue so that the iPad loads the iPad .xibs?**

Thanks!

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.
    self.viewController = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:[NSBundle mainBundle]] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
Was it helpful?

Solution

In my app, I do it like this. I create separate .h, .m and XIB files for iPad views and then in AppDelegate I simply make an if condition, which decides which view controller it will show.
Btw. I don't use those suffixes on XIBs, I name them to what I want to.

My AppDelegate.h file (a part of it)

 @class FirstViewController;
 @class FirstIpadViewController;    
 .......
 .......
 @property (nonatomic, retain) IBOutlet FirstViewController *viewController;
 @property (nonatomic, retain) IBOutlet FirstIpadViewController *ipadViewController;

My AppDelegate.m file (a part of it)

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
} else {
    self.window.rootViewController = self.ipadViewController;
    [self.window makeKeyAndVisible];
}

This should definitely do it. Just change the class and the property in .h file, to your view controller and you should be good to go :)

EDIT

I have just found out how to do it. And the proper naming conventions are _iPhone and iPad. This is basically the same as I posted above, only change is that it will have the same .h and .m files, but different XIBs.

In the AppDelegate .m file

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

OTHER TIPS

This solution works with Storyboards while using your own custom xibs. It reuses the same .h & .m files for the views:

  • ViewController.h
  • ViewController.m
  • ViewController.xib
  • ViewController~iPad.xib

In your ViewController.m file add:

- (id)initWithCoder:(NSCoder *)aDecoder
    {
        if (self)
        {
            NSString* nibName = NSStringFromClass([self class]);
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
            {
                self = [super initWithNibName:nibName bundle:nil];
            }
            else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
            {
                self = [super initWithNibName:[nibName stringByAppendingString:@"~iPad"] bundle:nil];
            }
            return self;
        }
        return self;
    }

Just check the xibs have the File's Owner View Controller Custom Class name set and the view outlet set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top