Experiment: Self Swapping Views?


Swap controllers from within a controller?

As per the Departments example, given the following scenario:

  1. Say the document had ONE NSBox called ‘contentBox’, this will serve as the viewing pane for BOTH views.
  2. App starts and shows the ‘Departments’ view controller
  3. ON the Departments View, there’s a button called ‘Add Employee’
  4. You press this, it tells the document to setContentView of the Documents contentBox to the EmployeeViewController

how is this achieved? I don’t get how to get the departments view to [[Document contentBox]setContentView: employeeView];

i.e. how do you call up through the heirachy of views to tell the document to do this?


Never mind, I sussed it out.

I used a few of the previous challenges etc… to achieve this… What I ended up doing was:

  1. In the Departments VC, I added the following:

- (MyDocument *)document { return [[[[self view]window]windowController]document]; }

  1. In the document, I changed the IBOutlet for my contentBox to:

@property (nonatomic, retain) IBOutlet NSBox *contentBox;

  1. To actually SWAP the views out I:


  • (IBAction)addEmployee:(id)sender {

    ContentManagingViewController *vc;

    vc = [[EmployeesVC alloc] init];
    [vc setManagedObjectContext:[self managedObjectContext]];

    NSView *v = [vc view];
    [[[self document] contentBox]setContentView:v];


THis appeared to work :slight_smile:

Can someone correct me if this is the wrong way of going about it?


Just to add, I’m going to beef it up a little and instead of having an init in the EmployeeVC, I’ll add a initWithCompany and initManagerWithCompany so an employee can be added to a company directly.


I just had another thought, is using NSSNotificationCentre a better option for doing this?

Which is the most common way of dealing with this situation?


Right… Now that I’ve had a little more time to go back on this, I’ve managed to sort it out nicely. For those interested, here’s what I did to this example AFTER it was finished and working to achieve what I was talking about:

Changes to NIB’s:

  1. EmployeeView: I added a button labelled: ‘Done’… i.e. done editing the employees, so its safe to go back to the Departments View
  2. DepartmentsView: Added a button labelled ‘Manage Employees’ under Staff area on the right i.e. to go to the EmployeesView

Code Changes:


@property (readonly, strong) MyDocument *document;
-(MyDocument *)document;


- (MyDocument *)document{
    return [[[[self view]window]windowController]document];


- (IBAction)showManageEmployees:(id)sender;


- (IBAction)showManageEmployees:(id)sender {
    [[self document] flipViewController:1]; //1 is the tag of the dropdown list corresponding to the EmployeesVC in the array


- (IBAction)showDepartmentsView:(id)sender;


- (IBAction)showDepartmentsView:(id)sender {
    [[self document] flipViewController:0]; //0 is the tag of the dropdown list corresponding to the DepartmentsVC in the array

For simplicity, what I did was mimic what the changeViewController void did in the MyDocument.h/.m i.e.




    ManagingViewController *vc = [viewControllers objectAtIndex:viewNumber];
    [self displayViewController:vc];

It’s slightly disheartening that someone from the BNR team didn’t take a second to comment or at least point me in the right direction. The thread generated enough views to catch peoples attention so I personally feel that it was warranted.

Anyway, I hope this helps whoever else was interested.