Multiple Views Using One Window

  • Hi everyone, I have been programming for a short while and have been
    picking it up quite quickly but I am now stuck. I have 3 nib files,
    one being the main window, and the other two as custom views. The
    main window has an NSBox and Next/Cancel buttons at the bottom of the
    window. I want to load a different view, (which are stored in their
    own nibs), into the NSBox each time I press 'Next'. I know I have to
    load the nib with [NSBundle loadNibNamed:name owner:_owner] but how
    do I get that information into the NSBox of the Main Window?

    Any help would be greatly appreciated.
  • Look at NSTabView.

    --Kyle Sluder
  • On Jan 18, 2008, at 11:32 PM, Philip Bridson wrote:

    > Hi everyone, I have been programming for a short while and have been
    > picking it up quite quickly but I am now stuck. I have 3 nib files,
    > one being the main window, and the other two as custom views. The
    > main window has an NSBox and Next/Cancel buttons at the bottom of
    > the window. I want to load a different view, (which are stored in
    > their own nibs), into the NSBox each time I press 'Next'. I know I
    > have to load the nib with [NSBundle loadNibNamed:name owner:_owner]
    > but how do I get that information into the NSBox of the Main Window?
    >
    > Any help would be greatly appreciated.

    Hi Philip,

    The NSBox is itself a subclass of NSView. So, you can utilize a
    combination of removeFromSuperview and addSubview. For example, you
    could bind the box  (or create a connection in IB) to a variable in
    your main controller class ("mainBox"). Connect the "Next" button to a
    method that unloads the previous subview from that box, then adds the
    next subview ("updateSubview"). Below is some example code...

    NSBox *mainBox;
    NSView *currentSubview;

    NSView *subView1;
    NSView *subView2;
    NSView *subView3;

    - (IBAction) updateSubview:(id) sender
    {
    [currentSubview removeFromSuperview];
    currentSubview =  subViewX    // use some logic, index, etc. to
    determine the next desired subview, and copy it to the currentSubview
    variable
    [mainBox addSubview];
    }

    hth,

    Jaime Magiera

    Sensory Research
    http://www.sensoryresearch.net
  • On 19.01.2008, at 09:02, Jaime Magiera wrote:
    > - (IBAction) updateSubview:(id) sender
    > {
    > [currentSubview removeFromSuperview];
    > currentSubview =  subViewX             // use some logic, index, etc. to
    > determine the next desired subview, and copy it to the
    > currentSubview variable
    > [mainBox addSubview];
    > }

      Don't forget to retain the subviews. Adding a view to a superview
    retains it, and removing it again releases it, so if you want to
    remove a subview from one view and add it to another, you may want to
    [[myView retain] autorelease] it to make sure it doesn't get disposed
    before you've re-added it.

      All that is of course is only necessary if you haven't otherwise
    retained the view already, and only if you're running non-GC.

    Cheers,
    -- M. Uli Kusterer
    "The Witnesses of TeachText are everywhere..."
    http://www.zathras.de
previous month january 2008 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