Search code examples
cocoainterface-buildercocoa-bindingsnsviewibplugin

Binding a custom NSView: Does it demand creating an IBPlugin?


I have created a subclass of NSView to draw an image as a pattern:

@interface CePatternView : NSView
{
    NSImage*    image;
    id      observableObjectForImage;
    NSString*   keyPathForImage;
}

@end

I implemented the following to expose bindings:

+ (void)initialize
{
    // Expose the "image" binding to IB.
    [self exposeBinding:@"image"];  
}

- (Class)valueClassForBinding:(NSString *)binding
{
    if([binding isEqualToString:@"image"])
        return [NSImage class];
    return nil; // Unknown binding
}

Unfortunately, the image binding does not show up in Interface Builder.

Do I really have to create an IBPlugin to expose bindings in Interface Builder? That just seems way overkill for a custom view that I don't plan to reuse.


Solution

  • Answer to title: No, you can bind a custom view without an IB plug-in (by doing it in code).
    Answer to question in question body: Yes, you do need an IB plug-in to expose the binding in IB.

    Your code doesn't run inside Interface Builder unless you put it into Interface Builder, and that exposeBinding: message is your code. Therefore, you need to put it into Interface Builder. That means writing an IB plug-in.

    Also, IB plug-ins are not the same as the old IB palettes. Plug-ins require IB 3 and are much easier to create. Palettes require IB 2 and were painful to create.