Swapping contents of windows from nibs

  • Greetings.
    I've been trying to figure out how to do something for a little
    while, and maybe I'm asking too much, or maybe I'm just looking in
    the wrong documentation, but so far, I haven't found any way to do it...

    What I want to do is design several windows in Interface Builder, all
    the same size and shape, and programmatically swap out the contents
    of one for another--similar to the way a CardLayout works in Java.

    I know that I *could* just get the bounds for the open window, open
    the new one with them, and close the old one, but I would think that
    wouldn't be as seamless as just replacing the contents of the window...

    Any assistance is greatly appreciated.

    Timothy Collett
  • On Thu, November 8, 2007 8:54 am, Timothy Collett said:
    > Greetings.
    > I've been trying to figure out how to do something for a little
    > while, and maybe I'm asking too much, or maybe I'm just looking in
    > the wrong documentation, but so far, I haven't found any way to do it...
    >
    > What I want to do is design several windows in Interface Builder, all
    > the same size and shape, and programmatically swap out the contents
    > of one for another--similar to the way a CardLayout works in Java.

    I think 'windows' is probably the wrong term for what you are trying to
    do, if I'm understanding you correctly.  Check out NSTabView and see if it
    does about what you are looking for.  (This would put everything in one
    window, but allow you to swap out the contents with alternate contents.)

    Daniel T. Staal

    ---------------------------------------------------------------
    This email copyright the author.  Unless otherwise noted, you
    are expressly allowed to retransmit, quote, or otherwise use
    the contents for non-commercial purposes.  This copyright will
    expire 5 years after the author's death, or in 30 years,
    whichever is longer, unless such a period is in excess of
    local copyright law.
    ---------------------------------------------------------------
  • At 9:37 AM -0500 11/8/07, Daniel T. Staal wrote:
    > On Thu, November 8, 2007 8:54 am, Timothy Collett said:
    >> Greetings.
    >> I've been trying to figure out how to do something for a little
    >> while, and maybe I'm asking too much, or maybe I'm just looking in
    >> the wrong documentation, but so far, I haven't found any way to do it...
    >>
    >> What I want to do is design several windows in Interface Builder, all
    >> the same size and shape, and programmatically swap out the contents
    >> of one for another--similar to the way a CardLayout works in Java.
    >
    > I think 'windows' is probably the wrong term for what you are trying to
    > do, if I'm understanding you correctly.  Check out NSTabView and see if it
    > does about what you are looking for.  (This would put everything in one
    > window, but allow you to swap out the contents with alternate contents.)

    Note that NSTabView doesn't actually have to have visible tabs; you
    can just have a stacked set of views and switch between them at need.

    Dan

    --
    Daniel T. Griscom            <griscom...>
    Suitable Systems              http://www.suitable.com/
    1 Centre Street, Suite 204    (781) 665-0053
    Wakefield, MA  01880-2400
  • One approach is to use a tab-less tab view to hold all of your different "contents" in one window and just programatically switch tabs.
      However, assuming there is some reason you want to define "contents" in different windows (perhaps to keep different "contents" in different nibs), I must ask the following questions:

      Where have you looked in the documentation ?
      Do you have some objection to using the following: ?
      -(id) contentView  and
      -(void) setContentView:(NSView*) aView
      http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSWindow_Class/Reference/Reference.html#//apple_ref/occ/instm/NSWindow
    /setContentView
    :

      Try this (written in Mail):
      - (void)setContentOfWindow:(NSWindow *)destinationWindow usingSourceWindow:((NSWindow *)sourceWindow
      {
          id          contentView = [[sourceWindow contentView] retain];  // get the content view

          // remove the view from its window because a single view can’t be in two windows at once
          [contentView setContentView:[[[NSView alloc] initWithFrame:NSZeroRect] autorelease]];

          // replace the content of destinationWindow
          [destinationWindow setContentView:contentView];
          [contentView release];
      }
  • I actually use this:

            NSPoint point = NSMakePoint(0,0);

    // very important that we set the frame origin..
    [ contentView setFrameOrigin:point];

    NSView *superView = [ currentContentView superview ];

    NSRect bounds = [ currentContentView bounds ];

    NSRect frame = [ currentContentView frame ];

    // set the frame for the incoming view
    [ contentView setFrame:frame ];

    [ contentView setBounds:bounds ];

    // replace the content..
    [ superView replaceSubview:currentContentView with:contentView ];

    Not the best code.. but seems to work just fine for me :)

    Hasani

    On Nov 8, 2007, at 9:16 AM, Erik Buck wrote:

    > One approach is to use a tab-less tab view to hold all of your
    > different "contents" in one window and just programatically switch
    > tabs.
    > However, assuming there is some reason you want to define
    > "contents" in different windows (perhaps to keep different
    > "contents" in different nibs), I must ask the following questions:
    >
    > Where have you looked in the documentation ?
    > Do you have some objection to using the following: ?
    > -(id) contentView  and
    > -(void) setContentView:(NSView*) aView
    > http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSWindow_Class/Reference/Reference.html#/

    > /apple_ref/occ/instm/NSWindow/setContentView:
    >
    > Try this (written in Mail):
    > - (void)setContentOfWindow:(NSWindow *)destinationWindow
    > usingSourceWindow:((NSWindow *)sourceWindow
    > {
    > id          contentView = [[sourceWindow contentView]
    > retain];  // get the content view
    >
    > // remove the view from its window because a single view can’t
    > be in two windows at once
    > [contentView setContentView:[[[NSView alloc]
    > initWithFrame:NSZeroRect] autorelease]];
    >
    > // replace the content of destinationWindow
    > [destinationWindow setContentView:contentView];
    > [contentView release];
    > }
previous month november 2007 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    
Go to today