MacRuby can't link controller to new window xib
-
12-06-2021 - |
Frage
My Setup
- XCode 4.3.2
- MacRuby 0.12 (ruby 1.9.2) [universal-darwin10.0, x86_64]
- Latest nightly as of 4 Jun, 2012
- OS 10.7.3
Goal
Have a window with some controls in a separate XIB from MainMenu.xib and be able to open that window programmatically. I do not want it to open at launch.
Attempts
- I made a new xib (Woot.xib) and created a window in it
I made a new Ruby class
class WootController < NSWindowController attr_accessor :window def windowNibName return 'Woot' end end
- I tried to set the class of File's Owner in the Woot.xib to be WootController, but found that it will not if
< NSWindowController
is in my class definition. If I remove the< NSWindowController
from the class definition, then the outlets populate and I can link the window in XIB to the window outlet in my class. From inside my AppDelegate's
applicationDidFinishLaunching
method, I've triedAttempt
newWind = WootController.new puts newWind #outputs "#<AddCredentialsDialog:0x400191180>" newWind.window().makeKeyAndOrderFront(self) # results in no method error for nil
Attempt 2
newWind = WootController.initWithWindowNibName 'AddWindow' puts newWind #outputs "#<AddCredentialsDialog:0x400191180>" newWind.window().makeKeyAndOrderFront(self) # results in no method error for nil
Questions
- Why don't either of my attempts work? I've ready just about everything I can find on macruby and using NSWindowController.
- Why can't I link my class
WootController
if I have it inheriting fromNSWindowController
- Is there a different way to do this other than putting it all in MainMenu.xib?
Lösung
This solution works
nib = NSNib.alloc.initWithNibNamed('Woot', bundle: nil)
newWind = WootController.new
nib.instantiateNibWithOwner(newWind, topLevelObjects:nil)
newWind.showWindow(self)
Some things to note
In Macruby, if there are named parameters to a method signature, you must use them even if you just specify nil or the method signatures don't match up and you get a
no method
error.ie.
obj.foo('hello', to_whom: nil)
is not the same asobj.foo('hello')
If there are named parameters you must use parentheses.
ie. this
obj.foo('hello', to_whom: nil)
will work, not thisobj.foo 'hello', to_whom: nil