0
votes

In my application window I have two NSViews. On the left the NSView ("Menu") contains a few buttons. When one of the buttons is clicked it should change the contents of the right NSView ("Content").

For each of the views on the right I have a separate NSViewControllers that get loaded and their views gets added as a subview. When a further button gets pressed on the left the added subviews on the right should be removed and the new view should be loaded as a subview.

To accomplish this I load my Menu in AppDelegate with the following:

 MenuVC *menuSubView = [[MenuVC alloc] initWithNibName:@"MenuVC" bundle: nil];
    menuSubView.contentView = (NSView*)[self contentView];
    [[self menuView] addSubview:[menuSubView view]];

This works fine. As you can see I have a NSView pointer in the Menu VC which points to the contentView so that I can populate it with the subviews.

Now as a method for one of the button presses I do the following:

SomeContentVC *subView = [[SomeContentVC alloc] initWithNibName:@"SomeContentVC" bundle:nil];
    [self.contentView addSubview:[subView view]];

This does not work.

If I however add a subview from the awakeFromNib method of the MenuViewController implementation (in the case of default content when the app opens) it works. However when I try to remove that subview using

[[self.contentView setSubviews:[NSArray array]];

I can't. Interesting is also that if I try to count the number of subviews (even after having added one in the awakeFromNib method) it returns 0 subviews for self.contentView. Why? How can I get it to work properly?

Thanks

1
What's self.contentView? As far as I know NSViewController doesn't have such a property, unless you're creating it yourself.Vervious
In the AppDelegate it is an NSView pointer pointing to the right NSView (the content part of my application). In the MenuVC I also have a NSView pointer which gets populated from the AppDelegate when it created and I pass along AppDelegate's pointer so that I can populate the correct NSView in the main application window. Maybe this helps lh5.googleusercontent.com/-rsaZ6LZLeqM/T4PCBu-z43I/AAAAAAAAAAo/…Joseph

1 Answers

1
votes

The fact that messaging self.contentView achieves nothing except, for some things, returning 0 probably means that self.contentView is nil.

Do you perhaps have two instances of MenuVC by accident? Perhaps one instantiated in a NIB and one instantiated in code?

When in doubt, log everything. Log self in various methods. Log menuSubView just after you create it. Log menuSubView.contentView just after you assign it. Etc. Eventually, you'll probably see that you're interacting with different objects than you thought you were.