Generic iPad app not loading iPad.xib files?

I’m trying to understand why this is happening, but it seems that the iPhone.xib is loading instead of the iPad in the iPad version of my universal application.

I named my iPhone xibs the suffix ~ iphone.xib, and I left my iPads with only .xib. I read this because someone said I worked for them, but in my case it did not work for me!

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

* * Is there a way to fully confirm that it is downloading the iPhone version instead of the iPad version?

And is there any way to fix this problem so that iPad downloads 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; } 
+6
source share
2 answers

In my application, I do it like this. I create separate .h, .m and XIB files for iPad views, and then in AppDelegate I just make an if condition, which decides which view controller it will show.
Btw. I do not use these suffixes on XIB, I call them what I want.

My AppDelegate.h file (part of it)

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

My AppDelegate.m file (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]; } 

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

EDIT

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

In the file AppDelegate.m

  - (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; } 
+4
source

This solution works with Storiesboards when using your own xib. It reuses the same .h and .m files for views:

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

In the 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 make sure xibs has its own custom controller View Controller class and set viewpoint.

0
source

Source: https://habr.com/ru/post/911009/


All Articles