Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Universal app for iPad not loading iPad .xib files?

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;
}
like image 649
SimplyKiwi Avatar asked Mar 17 '12 21:03

SimplyKiwi


1 Answers

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;
}
like image 90
Dominik Hadl Avatar answered Oct 21 '22 03:10

Dominik Hadl