Resolved NSView swapping without using NSBox?

Discussion in 'Mac Programming' started by GorillaPaws, Oct 20, 2011.

  1. GorillaPaws, Oct 20, 2011
    Last edited: Oct 22, 2011

    GorillaPaws macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #1
    I'm trying to swap out views in a window. Each view will have it's own nib and NSViewController Subclasses. I've implemented this in the past using an NSBox's setContentView: method. I was wondering how to accomplish this without using an NSBox. If I have NSView instances viewA and viewB, and my main window has a custom view that's wired up to my viewA object in IB, how do I later swap it to a viewB?
     
  2. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #2
    Any NSView can be used in place of a NSBox, so you could just pull the container view, full of its subviews, right out of the window and drop the other view into its place (making sure that the pulled view is not lost as -removeFromSuperview results in a -release being sent to the pulled view.

    Alternately, you could just place both containing views in the window with one of them out of sight (negative coördinates or somesuch). This is what Apple used to do in classic Mac OS with buttons, et al. If you do this, take care that the resize mask prevents the lurking view from crawling into sight if your window is resizable.
     
  3. GorillaPaws thread starter macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    Thanks for taking the time to write your excellent response. It seems like just using an invisible NSBox is actually less complex than the alternatives.
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  5. Kenndac macrumors 6502

    Kenndac

    Joined:
    Jun 28, 2003
    #5
    Sure, it's a few lines of code but it's not like it's an algorithmic challenge, and if you encapsulate it correctly you only need to write it once. Hell, I bet NSBox/etc do this exact same thing internally.

    Code:
    NSView *superView = [viewA superview];
    NSRect frame = [viewA frame];
    
    [viewA removeFromSuperViewWithoutNeedingDisplay];
    
    [viewB setFrame:frame];
    [superView addSubView:viewB];
    
     
  6. GorillaPaws, Oct 23, 2011
    Last edited: Oct 23, 2011

    GorillaPaws thread starter macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #6
    You're right that it's not a terrible amount of code (although I'm pretty sure I'd need to set up autoresize masks and such in code as well with your approach). I am pretty sold on Wil Shipley's philosophy that "less code is better".

    I'm going to give @kainjow's approach a try as well. I didn't realize you could have invisible tabs. Tabs do seem to be following the intention of the design of the framework more closely than NSBox (which feels a bit like a hack) that I tend to think of as a tool for visually clustering controls in a UI more than as a vehicle for swapping large views onscreen.

    Thanks again to everyone who took the time to contribute. I really do appreciate the help and advice.
     

Share This Page