适用于iPad的通用应用程序未加载iPad.xib文件



我一直在想为什么会发生这种情况,但在我的通用应用程序的iPad版本中,它似乎加载的是iPhone.xib,而不是iPad。

我把我的iPhone命名为xib,后缀为~iPhone.xib,我把iPad的名字只剩下.xib。我读到这样做是因为有人说这对他们有效,但在我的情况下,这对我不起作用!

即使我对不同的.xib文件使用了~ipad.xib和~iphone.xib,它仍然会加载iphone版本!

**有没有办法完全确认它正在加载iPhone版本而不是iPad版本

有没有办法解决这个问题,让iPad加载iPad.xibs**

谢谢!

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

在我的应用程序中,我是这样做的。我为iPad视图创建了单独的.h、.m和XIB文件,然后在AppDelegate中,我只需设置一个if条件,该条件决定它将显示哪个视图控制器
顺便说一句,我不在XIB上使用这些后缀,我根据自己的意愿命名。

我的AppDelegate.h文件(它的一部分)

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

我的AppDelegate.m文件(它的一部分)

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

这绝对应该做到。只需将.h文件中的类和属性更改为视图控制器,就可以了:)

编辑

我刚刚发现了如何做到这一点。正确的命名约定是_iPhone和iPad。这和我在上面发布的基本相同,唯一的变化是它将具有相同的.h和.m文件,但不同的XIB。

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

此解决方案可在使用您自己的自定义xib的同时与Storyboards配合使用。它重复使用相同的.h&m个视图文件:

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

在ViewController.m文件中添加:

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

只需检查xib是否设置了文件的所有者视图控制器自定义类名和视图出口。

最新更新