我有一个名为 SlideOut 的 UIView 子类。我想在加载时捕获 View 的 IB 位置,所以我在我的实现中有这个:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
IBframe.origin.x = frame.origin.x;
IBframe.origin.y = frame.origin.y;
IBframe.size.height = frame.size.height;
IBframe.size.width = frame.size.width;
// Initialization code.
NSLog(@"init if self x: %f, y: %f", IBframe.origin.x, IBframe.origin.y);
}
NSLog(@"init x: %f, y: %f", IBframe.origin.x, IBframe.origin.y);
return self;
}
...在 .h 文件中有一个匹配的原型(prototype)。
启动模拟器后,我得到了所有其他诊断信息,但这些日志消息都没有被调用。那么实例实际上是如何初始化的呢?或者我错过了什么?位置功能有效,但随后失败,因为我没有捕捉到物体的实际框架。无论如何,这就是我知道我正确建立了 IB 连接的方式。
最佳答案
看这里:Subclassing UIView, "Methods to Override", from UIView Class Reference .
特别是:
initWithCoder: - Implement this method if you load your view from an Interface Builder nib file and your view requires custom initialization.
关于iphone - 在 Interface builder 中放置的 UIView 中,init *真的*发生在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4787411/