在视图控制器中动态加载 iPhone/iPad 的笔尖

发布于 2024-09-05 20:31:50 字数 557 浏览 5 评论 0原文

我已经使用 XCode 中类似向导的东西将 iPhone 应用程序转换为通用应用程序。

它构建得很好,但显然在某些区域看起来有点垃圾:)

我需要根据正在使用的设备加载笔尖。我不想使用 initWithNib 创建视图控制器,因为我已经有了用一些数据创建控制器的代码 (initWithMyLovelyData),这与 nib 加载没有任何关系。

我知道要找出您使用 UI_USER_INTERFACE_IDIOM() 的设备,因此我尝试覆盖实际视图控制器本身中的 initWithNibName ,假设它们以某种方式在内部被调用。但它不起作用,因为我想我不确定语法。

我已经尝试过了,

if(ipad..) self = [super initWithNibName:@"MyIpadNib" bundle:nibBundleOrNil];

但这不起作用:/

编辑 - 我知道我已经对此进行了大量编辑,在做了更多研究后使我的问题更加具体 - 抱歉!

I have converted an iPhone application using the wizard like thing in XCode into a universal app.

It builds fine but obviously looks a bit rubbish in some areas :)

I need to load nibs according to which device is being used. I dont wish to create my view controllers using initWithNib as I already have code to create the controllers with some data (initWithMyLovelyData) which doesnt do anything to do with nib loading.

I know to find out the device you use UI_USER_INTERFACE_IDIOM() so I tried overriding the initWithNibName within the actual view controllers themselves, assuming they get called internally somehow. But it's not working as I guess I am unsure of the syntax.

I have tried

if(ipad..) self = [super initWithNibName:@"MyIpadNib" bundle:nibBundleOrNil];

And that doesnt work :/

EDIT - I know I have massively edited this, made my question a bit more specific after doing some more research - apologies!

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

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

发布评论

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

评论(5

帅冕 2024-09-12 20:31:51

实际上,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-09-12 20:31:51

编辑:@Adam 下面的答案是正确的答案。

要确定要加载哪个笔尖,请执行以下操作,然后废弃 initWithMyLovelyData 方法并使用属性来设置数据。您应该能够轻松地将所有初始化代码移至属性设置方法中。

MyViewController *viewController;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    viewController = [[MyViewController alloc] initWithNibName:@"ipadNIB" bundle:nil];
} else {
    viewController = [[MyViewController alloc] initWithNibName:@"iphoneNIB" bundle:nil];
}

viewController.myLovelyData = someData;

EDIT: @Adam's answer below is the correct answer.

To determine which nib to load, do the following, and scrap your initWithMyLovelyData method and use a property to set the data. You should be able to easily move all your init code into the property setter method.

MyViewController *viewController;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    viewController = [[MyViewController alloc] initWithNibName:@"ipadNIB" bundle:nil];
} else {
    viewController = [[MyViewController alloc] initWithNibName:@"iphoneNIB" bundle:nil];
}

viewController.myLovelyData = someData;
街角卖回忆 2024-09-12 20:31:51

我只是将这两个方法放在名为 IPadHelper 的类中,并使用 addIPadSuffixWhenOnIPad 方法根据平台有条件地在两个笔尖之间进行选择,

+ (BOOL)isIPad{
    if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_2){
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
            return YES;
        }
    }
    return NO;
}

+ (NSString *)addIPadSuffixWhenOnIPad:(NSString *)resourceName{
    if([IPadHelper isIPad]){
        return [resourceName stringByAppendingString:@"-iPad"];
    }
    else {
        return resourceName;
    }   
}

请参阅此处 http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html 了解更多说明第一种方法...

I just put these two methods in a class called IPadHelper, and use the addIPadSuffixWhenOnIPad method to conditionally pick between two nibs depending on platform

+ (BOOL)isIPad{
    if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_2){
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){
            return YES;
        }
    }
    return NO;
}

+ (NSString *)addIPadSuffixWhenOnIPad:(NSString *)resourceName{
    if([IPadHelper isIPad]){
        return [resourceName stringByAppendingString:@"-iPad"];
    }
    else {
        return resourceName;
    }   
}

see here http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html for more explanation of the first method...

琴流音 2024-09-12 20:31:51

你可以鱼与熊掌兼得。只需添加一个包装 initWithNibName:bundle: 的方法并添加 myLovelyData 参数:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  myLovelyData:(id)data
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
    {
        // Custom initialization using myLovelyData
        //
    }
    return self;
}

You can have your cake and eat it too. Just add a method that wraps initWithNibName:bundle: and adds your myLovelyData parameter:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  myLovelyData:(id)data
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
    {
        // Custom initialization using myLovelyData
        //
    }
    return self;
}
甜尕妞 2024-09-12 20:31:51

我认为创建 C 文件会更好。
文件助手.h

#import <Foundation/Foundation.h>
BOOL isIPad();
NSString *addIPadSuffixWhenOnIPad(NSString *resourceName);

文件助手.m

#import "FileHelper.h"
BOOL isIPad() {
    if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_2) {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            return YES;
        }
    }
    return NO;
}

NSString *addIPadSuffixWhenOnIPad(NSString *resourceName) {
    if(isIPad()) {
        return [resourceName stringByAppendingString:@"-iPad"];
    }
    else {
        return resourceName;
    }   
}

I think it will be better to create C file.
FileHelper.h

#import <Foundation/Foundation.h>
BOOL isIPad();
NSString *addIPadSuffixWhenOnIPad(NSString *resourceName);

FileHelper.m

#import "FileHelper.h"
BOOL isIPad() {
    if (kCFCoreFoundationVersionNumber >= kCFCoreFoundationVersionNumber_iPhoneOS_3_2) {
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            return YES;
        }
    }
    return NO;
}

NSString *addIPadSuffixWhenOnIPad(NSString *resourceName) {
    if(isIPad()) {
        return [resourceName stringByAppendingString:@"-iPad"];
    }
    else {
        return resourceName;
    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文