Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:01:23+00:00 2026-06-12T23:01:23+00:00

I have a NSWindowController which contains the root view. The root view has 4

  • 0

I have a NSWindowController which contains the root view. The root view has 4 NSButtons, some text and images. Each button is bound to a NSViewController. When I click on one of the buttons, the root view is removed and the view bound to the NSViewController (let’s call it the subview) is displayed. In the subview, there is a NSButton which sends a notification to the window controller to restore the root view. Here is my code (I removed most of useless part)

  • WindowController.h

    @interface MainWindowController : NSWindowController {
    
    IBOutlet NSView*    myTargetView; // bound to the whole view of the window
    NSView*             viewRoot;
    NSViewController*   myCurrentViewController;
    }
    
    - (IBAction)buttonClicked:(id)sender; // Not shown in the implementation
    - (void)changeViewController:(NSInteger)buttonTag;
    - (void)restoreRootView;
    
    @end
    
  • WindowController.m

    - initWithPath:(NSString *)newPath
    {
      return [super initWithWindowNibName:@"MainWindow"];
    }
    
    - (void)windowDidLoad {
    
       vwRoot = [[[[self window] contentView] subviews] objectAtIndex:0];
    
        // set up notification observer, will call restoreRootView when receiving notification from NSViewController object
    }
    
    - (void)changeViewController:(NSInteger)buttonTag
    {
    
      [vwRoot retain];
    
      [vwRoot removeFromSuperview];
    
    
      if (myCurrentViewController != nil)
      [myCurrentViewController release];
    
     switch (buttonTag)
    
           {
            case kView1:
              {
                   View1Controller * viewOneController = [[View1Controller alloc]         initWithNibName:kViewOneTile bundle:nil];
    
            if (viewOneController != nil) {
    
                   myCurrentViewController = viewOneController;
                }
    
            break;
          }
    
          case kView2:
           {
    
               // and so on...  
    
           }
       }
    
    
       [myTargetView addSubview: [myCurrentViewController view]];
    
       [[myCurrentViewController view] setFrame: [myTargetView bounds]];
    
     }
    
    
    - (void)restoreRootView {
    
    
       [[myCurrentViewController view] removeFromSuperview];
    
        [myTargetView addSubview:vwRoot];
    
       [[vwRoot setFrame:[myTargetView bounds]];
     }
    

Unfortunately, when restoreRootView is called, the NSViewController‘s view is removed, but the root view is not displayed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:01:24+00:00Added an answer on June 12, 2026 at 11:01 pm

    I’ve recreated your code assuming that vwRoot is the same as the viewRoot declared in WindowController.h, and everything is correctly bound and non-nil; I’ve used a Text View as the target view, two buttons, and retained its reference while it’s replaced by another view (owned by a controller).

    I encountered the same problem but only with autolayout. When I disabled autolayout the code started working perfectly.

    The problem was in the constraints: when the vwRoot is removed, the constraints that define its position in myTargetView are removed. You must define them again, otherwise your view will be placed somewhere outside the visible area (in my case, with the top left corner on the bottom left corner of the window: so, nothing visible).

    The code for adding the constraints (setting the frame is unnecessary):

    [myTargetView addSubview:vwRoot];
    [vwRoot removeConstraints:vwRoot.constraints]; // eventually remove old w/h constraints
    
    // snap to left and right border
    [myTargetView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[vwRoot]-0-|"
                                                                         options:0
                                                                         metrics:nil
                                                                           views:NSDictionaryOfVariableBindings(vwRoot)]];
    
    // snap to top and bottom border
    [myTargetView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[vwRoot]-0-|"
                                                                         options:0
                                                                         metrics:nil
                                                                           views:NSDictionaryOfVariableBindings(vwRoot)]];
    

    The same should apply to - (void)changeViewController:(NSInteger)buttonTag: when you add myCurrentViewController.view, there shouldn’t be any constraint in place, so unless you have some code to fix that, when you resize your window your content view shouldn’t follow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have deployed numerous report parts which reference the same view however one of them
I have a document-based application with a view controller that contains a table, array
Suppose you have a window nib, owned by a NSWindowController which loads the nib.
So, I instantiate an NSWindowController, which in turn instantiates a .xib. The xib has
I have a subclass of NSWindowController that has its own nib and another class
I have the following code: @interface AXWindowController : NSWindowController { IBOutlet NSTextField *text; IBOutlet
In an application (OS X 10.6.7) I have a NSWindowController subclass which is initialized
I have a NSWindowController with a setCurrentView method in which I use the following
I have an NSDocument which has the following structure: @interface MyDocument : NSDocument {
I have a custom NSView (MyView), which is rendered by the NSViewCollectionItem. MyView has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.