[MacRuby-devel] sub classing with hot cocoa
Laurent Sansonetti
lsansonetti at apple.com
Wed Dec 3 14:08:51 PST 2008
I noticed a strange behavior. Including the HotCocoa::Behaviors module
in subclasses indeed seem to merge the appropriate custom methods.
>> class MyButton < NSButton; include HotCocoa::Behaviors; end
=> MyButton
>> b = MyButton.new
=> 0
>> b.on_action {}
=> 0
But extending an existing object does not work in the same way.
>> b2 = NSButton.new
=> #<NSButton:0x800523420>
>> b2.extend HotCocoa::Behaviors
=> #<NSButton:0x800523420>
>> b2.on_action {}
NoMethodError: undefined method `on_action' for #<NSButton:0x800523420>
from (irb):12
from /usr/local/bin/macirb:12:in `<main>'
>>
Laurent
On Dec 3, 2008, at 6:13 AM, Richard Kilmer wrote:
> class MyView < NSView
> include HotCocoa::Behaviors
> def drawRect(rect)
> #do something...
> end
> end
>
> That HotCocoa::Behaviors was what you were missing.
>
> On Dec 3, 2008, at 12:58 AM, John Shea wrote:
>
>> Hi Ben,
>> thanks for your answer.
>>
>> I tried that initially, but my subclass was having problems being
>> added to the layout view.
>>
>> Anyway - somehow I got something going a little further - the
>> important thing was to have a create method which has :
>> alloc.initWithFrame([0, 0, *GameSize]) in my NSView subclass...
>>
>> I did not get much further than painting black (transparency etc
>> did not work) - but its on the back burner while I experiment a bit
>> more with plain MacRuby.
>>
>> Cheers and thanks,
>> John
>>
>> On Dec 2, 2008, at 10:37 PM, Benjamin Stiglitz wrote:
>>
>>>> I am sure there is an easy answer to this question, but I have
>>>> not figured it out after some experimenting (nor can I find an
>>>> example in the examples)
>>>>
>>>> What do I do if i want to subclass (say) an NSView ? And yet
>>>> still employ that subclass with all the rest of the hot cocoa
>>>> magic? (so without using nib/xibs)
>>>>
>>>> I would do this to overwrite the drawRect callback for NSView for
>>>> example - perhaps to do some animation.
>>>
>>> class MyView < NSView
>>> def drawRect(r)
>>>
>>> end
>>> end
>>>
>>> or, even better in some cases
>>>
>>> a = view(:something)
>>> class << a
>>> def drawRect(r)
>>>
>>> end
>>> end
>>>
>>> -Ben
>>> _______________________________________________
>>> MacRuby-devel mailing list
>>> MacRuby-devel at lists.macosforge.org
>>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>>
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
More information about the MacRuby-devel
mailing list