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

妖精的绣舞 提交于 2019-12-21 07:31:18

问题


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:


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.




回答2:


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:


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.




回答4:


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



来源:https://stackoverflow.com/questions/605721/binding-a-custom-nsview-does-it-demand-creating-an-ibplugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!