Switch views

Discussion in 'Mac Programming' started by themacster298, Jul 22, 2012.

  1. themacster298 macrumors member

    Feb 20, 2011
    Hello, I am using Matt Gemells status item code and I am having rouble switching between two views.

    I have created an array that holds both NSViews that I want to switch to and a integer with the value of 0 in it.

    What I was trying to do was to create a button that, when pressed, changed the value of the integer to 1.

    It worked, but you must open and close the window for it to work properly.

    How would I make it work so that it switches instantly?

    - (void)toggleAttachedWindowAtPoint:(NSPoint)pt
        // Attach/detach window.
        if (!attachedWindow) {
            [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
            attachedWindow = [[MAAttachedWindow alloc] initWithView:[arrayviews objectAtIndex:viewhold]
            [attachedWindow makeKeyAndOrderFront:self];
        } else {
            [attachedWindow setAlphaValue:1.0];
            [[attachedWindow animator] setAlphaValue:0.0];
            //[attachedWindow orderOut:self];
            [attachedWindow release];
            attachedWindow = nil;
    -(IBAction)openprefs:(id)sender {
        viewhold = 1;
  2. chown33 macrumors 604

    Aug 9, 2009
    Put an NSTabView in your MAAttachedWindow. Add your subviews to the NSTabView as NSTabViewItems.

    Set the tab-type of the NSTabView to one of the NSNoTabsXXX tab-types (see NSTabViewType enum), so the tabs aren't drawn. Then switch between subviews by passing the integer to selectTabViewItemAtIndex: .

    This is a fairly common way to get multiple independent subviews, where only one can appear at a time.
  3. themacster298 thread starter macrumors member

    Feb 20, 2011
    I cannot thank you enough.

    But, just one more question.

    What if I wanted to have a view with a different size?
  4. chown33 macrumors 604

    Aug 9, 2009
    Describe what you want to happen.

    Is the window always the size of the largest view? Does the window change size to accomodate the larger view, and shrink to accomodate smaller views? In which directions does the size change, vertically or horizontally or both?
  5. themacster298 thread starter macrumors member

    Feb 20, 2011
    Well, the program's main window is a custom view that you can resize to your desire.

    What I am asking is, how can I switch between 2 different custom views (with different sizes) within the same .nib file?
  6. chown33 macrumors 604

    Aug 9, 2009
    Then if you need it to be a certain size, you will have to constrain it to be that certain size.

    I asked you describe what you want to happen. From the user's perspective, what happens on-screen when the two different-sized views appear and disappear. You haven't done that yet.

    I already described how to switch between two different views using an NSTabView. If both custom views are within the same .nib file, then add an NSTabView, configure its options, and put your custom subviews into it as tab-items.

    Have you tried any of this yet? Have you looked up any Apple sample code that uses NSTabView? Or are you waiting for a complete step-by-step answer that accounts for every possible detail?

Share This Page