macoscocoainitwithframe

Cocoa: initWithFrame from a custom view proxy is not being called


According to apple's document Creating a Custom View this method should be called if you're using a custom view proxy in the Interface Builder.

If you have not created an Interface Builder palette for your custom view, there are two techniques you can use to create instances of your subclass within Interface Builder. The first is using the Custom View proxy item in the Interface Builder containers palette. This view is a stand-in for your custom view, allowing you to position and size the view relative to other views. You then specify the subclass of NSView that the view represents using the inspector. When the nib file is loaded by the application, the custom view proxy creates a new instance of the specified view subclass and initializes it using the initWithFrame: method, passing along any autoresizing flags as necessary. The view instance then receives an awakeFromNib message.

Unfortunately, It's not been called in my case. Has anybody had to deal with this issue?


Solution

  • There's new behavior in recent versions of Xcode when deploying to recent versions of the OS. In the File inspector of the NIB, see if Runtime Behavior — Instantiation: Prefer coder is checked. If it is, uncheck it if you need/want -initWithFrame: called for your custom views. Or, leave it checked and take advantage of the fact that the NIB loading machinery will now call -initWithCoder:. Of course, you should then make sure your class property supports NSCoding by encoding and decoding any custom internal state that makes sense to serialize.