Binding a custom NSView: Does it demand creating a

2019-04-04 06:00发布

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.

4条回答
放荡不羁爱自由
2楼-- · 2019-04-04 06:15

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!

查看更多
姐就是有狂的资本
3楼-- · 2019-04-04 06:32

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

查看更多
▲ chillily
4楼-- · 2019-04-04 06:33

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.

查看更多
一夜七次
5楼-- · 2019-04-04 06:39

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.

查看更多
登录 后发表回答