在视图控制器中动态加载 iPhone/iPad 的笔尖
我已经使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
实际上,Apple 会自动完成所有这些工作,只需命名您的 NIB 文件:
并使用最少的代码加载您的视图控制器:
Actually, Apple does all this automatically, just name your NIB files:
and load your view controller with the smallest amount of code:
编辑:@Adam 下面的答案是正确的答案。
要确定要加载哪个笔尖,请执行以下操作,然后废弃
initWithMyLovelyData
方法并使用属性来设置数据。您应该能够轻松地将所有初始化代码移至属性设置方法中。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.我只是将这两个方法放在名为 IPadHelper 的类中,并使用 addIPadSuffixWhenOnIPad 方法根据平台有条件地在两个笔尖之间进行选择,
请参阅此处 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
see here http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html for more explanation of the first method...
你可以鱼与熊掌兼得。只需添加一个包装 initWithNibName:bundle: 的方法并添加 myLovelyData 参数:
You can have your cake and eat it too. Just add a method that wraps initWithNibName:bundle: and adds your myLovelyData parameter:
我认为创建 C 文件会更好。
文件助手.h
文件助手.m
I think it will be better to create C file.
FileHelper.h
FileHelper.m