Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Main Window (App Delegate) from other class (subclass of NSViewController)?

Tags:

I'm trying to change my windows content, from other class , that is subclass of NSViewController.I'm trying code below, but it doesn't do anything.

[NSApplication sharedApplication]mainWindow]setContentView:[self view]]; //code in NSViewController  [NSApplication sharedApplication]mainWindow] // returns null 

I tried to add

[window makeMainWindow]; 

in App Delegate class, but it won't help.

Did I miss something?

P.S. Also I'm using code below to call any delegate function in my class,

 [(appDelegate *) [[NSApplication sharedApplication]delegate]MyMethod]; 

but I wonder is there something better, wihtout importing delegate class. Something like this

[[NSApplication sharedApplication]delegate]MyMethod]; 

(it gives warning)

like image 784
User1234 Avatar asked Oct 01 '11 12:10

User1234


People also ask

What is difference between AppDelegate and SceneDelegate?

AppDelegate is responsible for handling application-level events, like app launch and the SceneDelegate is responsible for scene lifecycle events like scene creation, destruction and state restoration of a UISceneSession.

How do I access AppDelegate in Swift?

You could access the AppDelegate through the UIApplication: let delegate = UIApplication. sharedApplication(). delegate as AppDelegate let deviceToken = delegate. deviceToken.

What is app delegate in iOS?

So an app delegate is an object that the application object can use to do certain things like display the first window or view when the app starts up, handle outside notifications or save data when the app goes into the background.

What is the use of SceneDelegate in Xcode 11?

The AppDelegate will be responsible for the application lifecycle and setup. The SceneDelegate will be responsible for what is shown on the screen (Windows or Scenes) handle and manage the way your app is shown.


2 Answers

For the mainWindow method the docs say:

This method might return nil if the application’s nib file hasn’t finished loading, if the receiver is not active, or if the application is hidden.

I just created a quick test application and I placed the following code:

NSLog(@"%@", [[NSApplication sharedApplication] mainWindow]); 

into my applicationDidFinishLaunching:aNotification method, and into an action method which I connected to a button in the main window of my application.

On startup, the mainWindow was nil, but when I click the button (after everything is up and running and displayed), the mainWindow was no longer nil.

NSApplication provides other methods which you may be useful to you:

  • - windows - an array of all the windows;
  • – keyWindow - gives the window that is receiving keyboard input (or nil);
  • – windowWithWindowNumber: - returns a window corresponding to the window number - if you know the number of the window whose contents you wish to replace you could use this;
  • – makeWindowsPerform:inOrder: - sends a message to each window - you could use this to test each window to see if it's the one you are interested in.

With regard to calling methods on the delegate, what you say gives a warning works fine for me. For example, this works with no warnings:

NSLog(@"%@", [[[NSApplication sharedApplication]delegate] description]); 

What exactly is the warning you receive? Are you trying to call a method that doesn't exist?

like image 169
Maurice Kelly Avatar answered Nov 10 '22 01:11

Maurice Kelly


Fighting with MacOS just figured this out.

Apple's quote:

mainWindow

Property

The app’s main window. (read-only)

Discussion

The value in this property is nil when the app’s storyboard or nib file has not yet finished loading. It might also be nil when the app is inactive or hidden.

If you have only one window in your application (which is the most used case) use next code:

NSWindow *mainWindow = [[[NSApplication sharedApplication] windows] objectAtIndex:0]; 

Promise it won't be nil, if application has windows.

like image 38
Borzh Avatar answered Nov 10 '22 01:11

Borzh