Os x equivalent to ios rootViewController

What would be the equivalent of OS X for ios' (Ruby style):

@window.rootViewController = NSViewController.alloc.initWithNibName(nil, bundle: nil) 

There are two aspects:

  • regarding lack of rootViewController in os x
  • fulfills the initWithNibName equivalent (nil, bundle: nil), which fails on os x

I am trying to create a window in code (without nib) ... and follow the Pragmatic Programmer's guide for RubyMotion (written for iOS).

+4
source share
3 answers

In OS X applications, there is no concept of "rootViewController". This is because applications consist of one or more windows and / or a menu bar, none of which are the β€œroot”.

However, you can find a window controller, starting with a view controller:

 [[[self view] window] delegate]; 

If you want to call custom methods on a window controller, it might be better to create a delegate so that the assumptions about the controller aren't in trouble.

the equivalent of NSViewController.alloc.initWithNibName(nil, bundle: nil) would be:

[[NSViewController alloc] initWithNibName:nil bundle:nil];

The method call is enclosed in square brackets, methods with several parameters are just label1:parameter1 label2:parameter2...

Usually, however, you will have your own subclass of NSViewController , which you will create instead.

+3
source

In my case, I had the only view controller that belonged to the window, so the following code worked for me:

 let viewController = NSApplication.sharedApplication().keyWindow!.contentViewController as! MyViewController 

where MyViewController is my subclass of NSViewController .

+3
source

If you want to reset rootViewController, as in iOS, you can use this code:

 let storyBoard = NSStoryboard(name: NSStoryboard.Name(rawValue:"Main"), bundle: Bundle.main) let homeViewController = storyBoard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "SIHome")) as! HomeViewController NSApp.keyWindow?.contentViewController = homeViewController 
  • NSApp is a shortcut for NSApplication.shared()
  • rootViewController called contentViewController on OSX
+1
source

Source: https://habr.com/ru/post/1495384/


All Articles