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

  1. I made a new xib (Woot.xib) and created a window in it
  2. I made a new Ruby class

    class WootController < NSWindowController
        attr_accessor :window
        def windowNibName
             return 'Woot'
        end
    end
    
  3. 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.
  4. From inside my AppDelegate's applicationDidFinishLaunching method, I've tried

    Attempt

    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

  1. Why don't either of my attempts work? I've ready just about everything I can find on macruby and using NSWindowController.
  2. Why can't I link my class WootController if I have it inheriting from NSWindowController
  3. Is there a different way to do this other than putting it all in MainMenu.xib?
War es hilfreich?

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

  1. 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 as obj.foo('hello')

  2. If there are named parameters you must use parentheses.

    ie. this obj.foo('hello', to_whom: nil) will work, not this obj.foo 'hello', to_whom: nil

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top