我所有的 IBOutlet 在 viewDidLoad 中都为零 [英] All my IBOutlet are nil in viewDidLoad
问题描述
我创建了一个 UIViewController
,我们可以调用它MyViewController1
.当我调用 MyViewController1
时,我所有的 IBOutlet
在 viewDidLoad
中都是 nil(在其余的代码中也是如此).
I've created a UIViewController
that we can call MyViewController1
. When I call MyViewController1
, all my IBOutlet
are nil in viewDidLoad
(and in the rest of the code too).
当我创建这个控制器时MyViewController1 *vc = [[MyViewController1 alloc] init]
,
When I create this controller by doing
MyViewController1 *vc = [[MyViewController1 alloc] init]
,
如果我将 MyViewController1
替换为另一个,例如 MyViewController2
,它会起作用.所以我想问题确实出在 MyViewController1
中.
if I replace MyViewController1
by an other one, for example MyViewController2
, it works. So I guess the problem is really in MyViewController1
.
您可能想知道的最后一件事是,MyViewController1
实际上是 MySuperViewController1
的子类,它是一个 UIViewController
.
Last thing you might want to know, is that MyViewController1
is actually a subclass of MySuperViewController1
that is a UIViewController
.
感谢您的帮助!
编辑
我意识到我的情况可能更复杂.这是我的确切文件:
I realized my case was maybe more complicated. Here are my exact files :
//MySuperViewController1
// MySuperViewController1
MySuperViewController1.h
MySuperViewController1.m
MySuperViewController1.xib
//MyViewController1
// MyViewController1
MyViewController1.h
MyViewController1.m
所以笔尖属于超类,而不是子类.我可以吗?
So the nib belongs to the superclass, and not the subclass. Can I do that ?
推荐答案
你可能应该使用:
MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"MyViewController1" bundle:nil]
调用 init
不会与您的 xib 文件进行匹配,也不会分配您的不同 IBOutlet
calling init
won't do the match with your xib file and won't alloc your differents IBOutlet
有两种可能的解决方案:
There are two possibles solutions :
首先使用 super nibName 调用 init :
First is calling init with super nibName :
MyViewController1 *vc = [[MyViewController1 alloc] initWithNibName:@"MySUperViewController1" bundle:nil]
第二个是在子初始化方法中调用超级initWithNibName:
:
The second is calling the super initWithNibName:
in child init method :
-(id)init {
if (self = [super initWithNibName:@"MySuperViewController1" bundle:nil]) {
// Init
}
return self;
}
这篇关于我所有的 IBOutlet 在 viewDidLoad 中都为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!