[MacRuby-devel] Problem with a window controller

Andy Park sohocoke at gmail.com
Sun Dec 2 07:02:42 PST 2012


On Sunday, 2 December 2012, david kramf wrote:

> Thank you very much Andy and Jim
> I followed your remarks , played  with the code and I am still surprised.
> It turns out that awakeFromNib is called before applicationDidFinishLaunching
> so there MyController is already initialized and has the correct window
> as the his instance variable. NSWindow has both a delegate protocol and a
> Controller class and so I called setDelegate and setController . Everything
> seems OK but the methods willLoad and didLoad are not called although
> clearly my class is both the delegate and the controller of my window . So
> I am still left asking what am I doing wrong (??).
>
>
There are a few things about the code below that seems to be throwing you
down a rabbit hole, discussed here.
http://www.cocoabuilder.com/archive/cocoa/93361-awakefromnib-and-windowwillload-basic-question.html

When using xibs, the delegate of view objects such as windows are
conventionally set in the xib
by connecting the outlet. Try replacing the contents of your awakeFromNib
with traces
of the window's delegate and controller, set up your window's ibloutlet to
the controller in your
Xib, and see what happens.

As for the window delegate methods, the mailing list thread suggests you
should avoid
relying on them to signal view-level events.



> Are there MacRuby code samples I can use. I read Lim,Chueng and McAnally
> book. I wonder where can I find more samples. I am also not sure I am doing
> everything correct with the IB . My blue box that represents MyController
> list "showWindow: " as a "Received Action"  , but I could not  connect it
> to any element of window draw in the XIB file.
>

ShowWindow is probably not meant to be an IBAction that you're expected to
connect with a control.
This sounds like the macruby script that parses IBActions is taking the
method to be an IBAction due
to the signature being like so, when you just wanted to implement a
delegate method.

>
> David
>
> class AppDelegate
>     attr_accessor :window
>     def applicationDidFinishLaunching(a_notification)
>         puts " Insert code here to initialize your application"
>         wCtrl = @window.delegate
>         puts "no delegate" if wCtrl == nil
>         win = wCtrl.window
>         puts "win is nil" if win == nil
>         puts "title of window is #{win.title}"
>         ctrl = @window.windowController
>         puts "no controller" if ctrl == nil
>         puts "class of delegate is #{wCtrl.class}"
>         puts "class of controller is #{ctrl.class}"
>         puts "both are equal " if wCtrl == ctrl
>         x = ctrl.showWindow
>         puts "x class is #{x.class} " unless x == nil
>
>
>         #puts "window nil " if win == nil
>         #wCtrl.close
>     end
> end
> class MyController < NSWindowController
>     attr_accessor :window
>
>
>     def initialize
>         puts "in initialize"
>         initWithWindowNibName("tow")
>         #puts "after initialization window is #{@window.title}"
>     end
>
>
>     def routine
>         puts "in routine"
>     end
>
>
>     #def initWithWindow(window)
>     #   puts "in initWithWindow"
>     #   super(@window)
>     #end
>
>
>     def awakeFromNib
>         @window.setDelegate(self)
>         @window.setWindowController(self)
>         puts " at end of awake from nib. title is #{@window.title}"
>     end
>
>
>     def windowWillLoad
>         puts "window  will be soon loaded"
>     end
>
>
>     def windowDidLoad
>         puts "window loaded"
>     end
>     def windowTitleForDocumentDisplayName(displayName)
>         "Hello World"
>     end
>
>
>     def showWindow
>        puts "in showWindow"
>        super(self)
>     end
>
>
>     def close
>         puts "in close window is #{@window.title}"
>         super
>     end
>
>
> end
>
>
> *at end of awake from nib. title is two*
> * Insert code here to initialize your application*
> *title of window is two*
> *class of delegate is MyController*
> *class of controller is MyController*
> *both are equal *
> *in showWindow*
> *x class is MyController *
>
>
>
>
> On Dec 1, 2012, at 3:00 AM, Andy Park wrote:
>
> Without being able to verify anything for accuracy at the moment, it looks
> like your window's delegate is not set to the controller at the time the
> events are occurring.
>
> Check if you need to set this - try tracing the window's delegate at
> different points of the controller's lifecycle.
>
> On 30 Nov 2012, at 00:50, david kramf <dakr.012 at gmail.com> wrote:
>
>
> Hi,
> In the copied below code only the awakeFromNib is executed . Can someone
> explain me what do I do wrong ?  Window is displayed and I expected all
> other methods to be called.
> Thanks, David
>
>
> class MyController < NSWindowController
>     attr_accessor :window
>
>     def awakeFromNib
>         @window.delegate = self
>         puts " at end of awake from nib. title is #{@window.title}"
>     end
>
>     def windowWillLoad
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20121203/dbac8ff7/attachment-0001.html>


More information about the MacRuby-devel mailing list