initWithNibName没有被调用 [英] initWithNibName is not getting called
问题描述
我需要将一些自定义逻辑放入我的iPhone应用程序,这样根据您运行的iOS版本,选择一个不同的XIB文件(即iPhone或iPad将显示不同的XIB文件)。
I need to put it some custom logic into my iPhone app so that depending on what iOS version you are running, choose a different XIB file (i.e. iPhone or iPad will show different XIB files).
我已经从第一天构建了整个iPhone应用程序,它的一切都很好,在每个选项卡中使用tabbar控制器和标准导航控制器。
I had built the whole iPhone app from day one and its all good, using a tabbarcontroller and the standard navigation controllers in each tab.
所以我实现了:
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
方法,只是发现它没有被调用。 (我有一个断点和日志语句,没有任何获得命中)。
method, only to find that it does not get called. (I have a breakpoint and log statements in it and nothing gets hit).
有人知道为什么会这样吗? 或如何实现此功能?
Does anyone know why this might be? OR how can i achieve this functionality?
我在某处读到initWithNibName仅在调用它时调用,查看层次结构,这是真的吗?
I have read somewhere that initWithNibName is only called when you call it, i.e. when you programatically construct your view hierarchy, is this true??
感谢很多...
推荐答案
如果你在添加到导航控制器之前不这样做:
If you're not doing something like this before adding it to your navigation controller:
SomeViewController *someViewController = [[SomeViewController alloc] initWithNibName:...];
然后很有可能你正在覆盖错误的方法。
Then chances are good you're overriding the wrong method.
如果你的视图控制器存在于一个nib文件中,比如你的导航控制器的nib文件,它将作为一个新的视图控制器对象从nib文件中取消存档,而不是使用自己的nib文件创建,因此
If your view controller lives in a nib file, say your navigation controller's nib file, it will be unarchived from the nib file as a new view controller object, instead of being created with its own nib file, so the above method won't be called.
您将不得不重写 awakeFromNib
。
这篇关于initWithNibName没有被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!