开发者

Where does the init *really* happen in a UIView placed in Interface builder?

开发者 https://www.devze.com 2023-02-07 00:36 出处:网络
I have a subclass of UIView called SlideOut. I want to capture the IB placement of the view when it loads, so I have this in my implementation:

I have a subclass of UIView called SlideOut. I want to capture the IB placement of the view when it loads, so I have this in my implementation:

- (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;
}

... with a matching prototype in the .h file.

Upon starting up the simulator, I get all my other diagnostics, but neither of these log messages get called. So how does the instance actually get initialized? Or am I missing something? The positio开发者_JS百科n function works, but then fails because I haven't captured the actual frame of the thing. In any case, that's how I know I made the IB connections correctly.


Take a look here: Subclassing UIView, "Methods to Override", from UIView Class Reference.

In particular:

initWithCoder: - Implement this method if you load your view from an Interface Builder nib file and your view requires custom initialization.


Objects that are loaded from an xib are actually unarchived. Thus, you should be using the initWithCoder: method. Alternatively, you may way to look at awakeFromNib instead.


I believe the function that gets called when the view is loaded from a NIB is initWithCoder not initWithFrame

0

精彩评论

暂无评论...
验证码 换一张
取 消