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)
For the mainWindow method the docs say:
I just created a quick test application and I placed the following code:
into my
applicationDidFinishLaunching:aNotificationmethod, and into an action method which I connected to a button in the main window of my application.On startup, the
mainWindowwas nil, but when I click the button (after everything is up and running and displayed), themainWindowwas no longer nil.NSApplicationprovides 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:What exactly is the warning you receive? Are you trying to call a method that doesn’t exist?