Re-using a custom view

  • Hello,

      How can I create, say, a Nib with one NSView in it with a few
    buttons and a field, and then install that NSView into other windows?
    I'd like to re-use the code and view.

      What's the easiest way?  The solutions I'm finding out there are
    kind of complicated.  I'm looking for something nice and simple.  :-)

    Thanks,
      Sanford
  • On Dec 8, 2006, at 10:39 AM, Sanford Selznick wrote:

    > Hello,
    >
    > How can I create, say, a Nib with one NSView in it with a few
    > buttons and a field, and then install that NSView into other
    > windows? I'd like to re-use the code and view.
    >
    > What's the easiest way?  The solutions I'm finding out there are
    > kind of complicated.  I'm looking for something nice and simple.  :-)

    1) Put that view into a nib.
    2) Define a "controller" class and make that class the owner of the nib.
    3) Add an outlet from the "controller" class (files owner) to the
    view in the nib.
    4) Have the "controller" class use NSNib to instantiate new instances
    of the nib (make sure to correctly manage top-level objects of the nib).
    5) Each time you need the view instantiate a new "controller" class
    and do what you want with the view it has a reference to.

    That is one way out of many...

    -Shawn
  • At 10:54 AM -0800 12/8/06, Shawn Erickson wrote:
    >> What's the easiest way?  The solutions I'm finding out there are
    >> kind of complicated.  I'm looking for something nice and simple.
    >> :-)
    >
    > 1) Put that view into a nib.
    > 2) Define a "controller" class and make that class the owner of the nib.
    > 3) Add an outlet from the "controller" class (files owner) to the
    > view in the nib.
    > 4) Have the "controller" class use NSNib to instantiate new
    > instances of the nib (make sure to correctly manage top-level
    > objects of the nib).
    > 5) Each time you need the view instantiate a new "controller" class
    > and do what you want with the view it has a reference to.

    Wonderful.  Thank you very much.

    Is 5 as simple as calling NSView's addSubView on the "host" view?
    Will that fix up field tab ordering and everything?

    Thanks,
      Sanford
  • At 10:54 AM -0800 12/8/06, Shawn Erickson wrote:

    >> ...
    >> 5) Each time you need the view instantiate a new "controller"
    >> class and do what you want with the view it has a reference to.

    On Dec 8, 2006, at 2:23 PM, Sanford Selznick wrote:

    > Wonderful.  Thank you very much.
    >
    > Is 5 as simple as calling NSView's addSubView on the "host" view?
    > Will that fix up field tab ordering and everything?

    Yes, for the most part the key view loop will be automatically
    updated by the parent NSWindow whenever a subview is added.  This
    functionality was added to the AppKit in 10.4.  You can control this
    behavior by using NSWindow's autorecalculatesKeyViewLoop and
    setAutorecalculatesKeyViewLoop methods.

    The reason I said "for the most part" is because the auto calculation
    does its work based on the geometric order of the views inside the
    NSWindow (The same calculation is performed if the window has no
    initialFirstResponder).  This is usually acceptable, but in cases
    where it's not (or when you're targeting Panther + earlier), you can
    explicitly use setNextKeyView:.

    Some ADC links for more reference:

    NSView: <http://developer.apple.com/documentation/Cocoa/Reference/
    ApplicationKit/Classes/NSView_Class/index.html
    >

    NSWindow: <http://developer.apple.com/documentation/Cocoa/Reference/
    ApplicationKit/Classes/NSWindow_Class/index.html
    >

    - Sean
previous month december 2006 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Go to today