0

I am new to OS X application. In iOS there is methods like :

1. self.window.rootViewController = self.viewController;
2. [self.window addSubview:self.viewController.view];

to add view controller in window or

3. DefaultViewController *objDefault = [[DefaultViewController alloc] initWithNibName:@"DefaultViewController" bundle:nil];
   [self.navigationController pushViewController:objDefault animated:TRUE];
4. DefaultViewController *objDefault = [[DefaultViewController alloc] initWithNibName:@"DefaultViewController" bundle:nil];
   [self presentViewController: objDefault animated:TRUE completion:nil];

to push on next view controller.

My question is that in OS X is there any method like above to add new view controller to window or push on next view controller..?

Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
VRAwesome
  • 4,721
  • 5
  • 27
  • 52

2 Answers2

0

Cocoa and Cocoa-touch have a little different ways to change views on the window. To change views you need to programmatically remove old subview and add new one.

- (void)addNewSubview:(NSView *)view // NSWindowController subclass implementation file
{
  [_subview removeFromSuperview];
  NSView *contentView = (NSView *)self.contentView;
  [contentView addSubview:view];
  _subview = view;
}

or simple [contentView replaceSubview:_subview with:view];

Since OS X 10.10 there's an opportunity to use storyboards something like in iOS.

UPD

To awake window from the application delegate class create your NSWindowController-subclass instance and show the window (it's mostly like in iOS' before-storyboards era).

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  CustomWindowController *controller = [[CustomWindowController alloc] initWithWindowNibName:@"CustomWindowController"];
  [controller showWindow:nil];
  [controller.window makeKeyAndOrderFront:nil];
}
Daniyar
  • 2,975
  • 2
  • 26
  • 39
  • I have subclass of NSViewController, and I want to add this one in `- (void)applicationDidFinishLaunching:(NSNotification *)aNotification`. Then How it can be achieved..? – VRAwesome Feb 17 '15 at 08:36
0

If you are using storyboards and segues to show a new NSViewController, then you might need to close the old view controller. Here is how I do it:

- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender
{
    [self.view.window close];
}

Each view controller itself creates a window, so you need to close the old one before showing the new one.

Borzh
  • 5,069
  • 2
  • 48
  • 64