Universal app for iPad not loading iPad .xib files?

前端 未结 2 415
死守一世寂寞
死守一世寂寞 2020-12-30 14:08

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 on

2条回答
  •  轮回少年
    2020-12-30 15:09

    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.

提交回复
热议问题