views:

1426

answers:

4

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.

+1  A: 

No, you can use the method

bind:toObject:withKeyPath:options:

to establish your binding programmatically. I believe you do have to create an IB palette to get the bindings to appear in Interface Builder, but for a one-off class I don't intend to reuse, I've never bothered.

Alex
I know I can bind programmatically, but I really need to do it in IB, this time. Anyway, thank you indeed for your answer, Alex.
Renaud Pradenc
+2  A: 

If you can manage to do the bindings manually you will save yourself a lot of time. Creating custom IB palettes is a lot of work compared to a few lines of manual binding code. But, if your needs require a custom IB palette then I would start by reviewing what the NSView subclass will require, coding-wise. A great place to start looking is Crawford's website on bindings:

http://homepage.mac.com/mmalc/CocoaExamples/controllers.html

I've used it a lot over the past couple years, it has helped a lot with my custom IB palette objects and with binding issues in general. There is an example on his site specifically detailing custom NSView's with custom bindings.

Something else to note, is that your custom view will also have to work in the Interface Builder environment. There are a few small fixes that need to be put into place in your bindings code in your custom NSView object so that it functions and binds properly in Interface Builder. These details are also noted on Crawford's site:

http://homepage.mac.com/mmalc/CocoaExamples/controllers.html#ibBindings

Evan
+3  A: 

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.

Peter Hosey
+2  A: 

I simply bound my controller object to my view object using a different, standard binding (say, toolTip), then edited the XIB file using a text editor and altered the XML manually.

Thereafter, the binding works correctly and even shows up in Interface Builder correctly to boot!

I just did that myself, because I didn't want to do an IB Plugin. +1!
Graham Lee