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

前端 未结 4 1140
闹比i
闹比i 2021-02-19 09:01

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

@interface CePatternView : NSView
{
    NSImage*    image;
    id      observableObjectForImag         


        
相关标签:
4条回答
  • 2021-02-19 09:22

    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.

    0 讨论(0)
  • 2021-02-19 09:25

    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.

    0 讨论(0)
  • 2021-02-19 09: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

    0 讨论(0)
  • 2021-02-19 09:38

    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!

    0 讨论(0)
提交回复
热议问题