加载不同的 Nib 文件

发布于 2024-11-14 14:58:08 字数 229 浏览 3 评论 0原文

我为 iPhone 和 iPad 创建了两个 nib 文件,因此我的应用程序将是通用的。

我使用这种方法来检查它是否是 iPad:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

但我不知道当它知道它是什么时如何加载正确的笔尖。

有谁知道相应地加载到 nib 文件的正确方法?

I created two nib files for the iPhone and iPad, so my app will be universal.

I use this method to check if it is an iPad:

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

but I don't know how to load the proper nib when it knows which it is.

Does anyone know the correct method to load to nib file, accordingly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

知你几分 2024-11-21 14:58:08

实际上,Apple 会自动完成所有这些工作,只需命名您的 NIB 文件:

MyViewController~iphone.xib // iPhone
MyViewController~ipad.xib // iPad

并使用最少的代码加载您的视图控制器:

[[MyViewController alloc] initWithNibName:nil bundle:nil]; // Apple will take care of everything

Actually, Apple does all this automatically, just name your NIB files:

MyViewController~iphone.xib // iPhone
MyViewController~ipad.xib // iPad

and load your view controller with the smallest amount of code:

[[MyViewController alloc] initWithNibName:nil bundle:nil]; // Apple will take care of everything
逆夏时光 2024-11-21 14:58:08

您的界面文件应该以不同的方式命名,这样类似的东西应该可以工作。

UIViewController *someViewController = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    someViewController = [[UIViewController alloc] initWithNibName:@"SomeView_iPad" bundle:nil];
}
else
{
    someViewController = [[UIViewController alloc] initWithNibName:@"SomeView" bundle:nil];
}

Your interface files should be named differently so something like this should work.

UIViewController *someViewController = nil;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    someViewController = [[UIViewController alloc] initWithNibName:@"SomeView_iPad" bundle:nil];
}
else
{
    someViewController = [[UIViewController alloc] initWithNibName:@"SomeView" bundle:nil];
}
缱绻入梦 2024-11-21 14:58:08

您应该使用初始化器 -[UIViewController initWithNibNamed:bundle:];
在您的 SomeViewController.m 中:

- (id)init {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (nil != (self = [super initWithNibName:@"SomeViewControllerIPad"])) {
            [self setup];
        }
    } else {
        if (nil != (self = [super initWithNibName:@"SomeViewControllerIPhone"])) {
            [self setup];
        }
    }
    return self;
}

You should use a initializer -[UIViewController initWithNibNamed:bundle:];.
In your SomeViewController.m:

- (id)init {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (nil != (self = [super initWithNibName:@"SomeViewControllerIPad"])) {
            [self setup];
        }
    } else {
        if (nil != (self = [super initWithNibName:@"SomeViewControllerIPhone"])) {
            [self setup];
        }
    }
    return self;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文