Universal app for iPad not loading iPad .xib files?

不羁的心 提交于 2019-11-30 07:28:53

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;
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!