我有一个我从UIView派生的类,我想为它创建一个-init类,如下所示:
- (id) init
{
if (self = [super init]) {
// my initializations here
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我知道一个事实 - 没有被调用.这是我在.h中定义的方式:
-(id)init;
Run Code Online (Sandbox Code Playgroud)
有谁知道它为什么不被调用?
附录:
我发现当我在initWithCoder中进行初始化时,它运行得很好.我添加了initWithFrame,但没有调用.
这是我在IB中指定的对象.
NJo*_*nes 13
有两个指定的初始化器UIViewController,UIView它们initWithCoder从中调用nib,并initWithFrame从代码中调用.Init不是这些对象的指定初始值设定项.
如果你想要涵盖两个基础,你可以做这样的事情:
-(void)initializationCodeMethod{
<#Initialization Code Here#>
}
-(id)initWithFrame:(CGRect)frame{
if ((self = [super initWithFrame:frame])){
[self initializationCodeMethod];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])){
[self initializationCodeMethod];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4186 次 |
| 最近记录: |