如何在通用 UIViewController 中创建仅适用于 iPAD 的 IBOutlet - 通用应用程序
我有 2 个 xib,一台用于 iPad,一台用于 iPhone。 然而,目前我只有一个视图控制器可用于这 2 个 xib,同时适用于 iPhone 和 iPad。
在我的 iPad Xib 中,我有一个不属于 iPhone xib 的 IBOutlet。 我应该如何定义那个出口? 我注意到,如果我放入 deallc 方法中,会出现类似这样的情况:
-(void) dealloc
{
[outletOnlyForIpad release]
}
应用程序在 iPhone 上崩溃。显然是因为它在 iPhone 上实例化得不好。 (我希望它保持为零,但事实并非如此)
我没有找到任何可以使用的预处理器宏,因此我可以声明该 Outlet 仅适用于 iPad。 唯一的方法是通过检查运行时类似:
isIpad()
[outletOnlyForIpad SomeMethodOnTheOutlet]
在我的控制器的每个地方?
I have 2 xibs, one for iPad and one for iPhone.
However, currently I have only one view controller for those 2 xibs that works for both iPhone&iPad.
Inside my iPad Xib I have an IBOutlet that doesn't belong to the iPhone xib.
How should I define that outlet ?
I notice that if I put inside my deallc method, something like this :
-(void) dealloc
{
[outletOnlyForIpad release]
}
The app crashes on the iPhone. Apparently cause it doesn't instantiates well on the iPhone. (I hoped it would stay nil, but it's not the case)
I didn't find any preprocessor macro that I can use so I can declare that Outlet only for iPad.
Is the only way to do it is by checking in runTime something like :
isIpad()
[outletOnlyForIpad SomeMethodOnTheOutlet]
In every place in my controller ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的代码是正确的。该插座应该保持
nil
,因此向其发送release
消息应该是无害的。我建议您检查此插座的设置位置并解决问题的原因,而不是解决该错误。您确定 iPhone 笔尖上没有连接任何东西吗?Your code is correct. That outlet should be staying
nil
, so sending it arelease
message should be harmless. Rather than work around the error, I suggest you check where this outlet is being set and fix the cause of the problem. Are you certain you aren't connecting anything to it in the iPhone nib?如果您想要单独的插座,您必须在运行时检查设备并处理插座。
如果我在不同设备上创建具有不同控件的应用程序,我倾向于以编程方式创建它们以避免此类连接问题。
You have to check at run-time for the device and handle the outlets then if you want separate outlets.
If I create an app with different controls on different devices, I tend to create them programmatically to avoid connection problems such as this.
在你的 dealloc 中以避免 iPhone 上的崩溃,并像往常一样在你的 iPad xib 中连接 IBOutlet:
in your dealloc to avoid the crash on iPhone, and connect the IBOutlet as usual in your iPad xib:
当您初始化类时,所有实例变量都会自动初始化为零。因此,这相当于添加:
到您的
init
方法中。当为 iPad 而不是 iPhone 加载视图时,此
nil
值将被覆盖(除非您有其他代码来设置它)。但是,您的[outletOnlyForIpad release]
崩溃表明outletOnlyForIpad
此时不是nil
,这意味着某些东西给它一个值。您需要找出这个东西是什么 - 它可能是您的界面构建器文件。When you initialize your class, all instance variables are automatically initialised to zero. Thus this is the equivalent of adding:
To your
init
method.This
nil
value will be overwritten when the view loads for the iPad but not the iPhone (unless you have other code that sets it). However your[outletOnlyForIpad release]
crash shows thatoutletOnlyForIpad
is notnil
at this point, which means that something has given it a value. You need to find out what this something is - it may be your interface builder file.