在视图控制器中为iPhone / iPad动态加载笔尖 [英] Dynamically load nib for iPhone/iPad within view controller

查看:96
本文介绍了在视图控制器中为iPhone / iPad动态加载笔尖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :)

我需要加载笔尖使用哪种设备。我不希望使用 initWithNib 创建我的视图控制器,因为我已经有代码来创建带有一些数据的控制器( initWithMyLovelyData )与nib加载无关。

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.

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

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.

我试过

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

这不起作用:/

编辑 - 我知道我已经大量编辑了这个,在做了一些更多的研究之后让我的问题变得更具体了 - 道歉!

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

推荐答案

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

@Adam's answer below is the correct answer.

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

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;

这篇关于在视图控制器中为iPhone / iPad动态加载笔尖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆