[MacRuby-devel] My class's initialize not called

steve ross cwdinfo at gmail.com
Thu Jan 21 16:09:46 PST 2010


So, this class doesn't reopen a Cocoa class, it subclasses it. Same anyhow? The real problem is that the class is instantiated as a side effect of nib loading, and the only place I've been able to successfully set initial state is awakeFromNib. I either can't, or don't know how to make the nib load process call a DuplicateCounterTextField.initWithSomeCoolState method. My impression is that awakeFromNib is a poor place for setting initial state because the order in which awakeFromNib's are called is undefined (or is it?). Not to overcomplicate things, but the controller needs to be able to tweak the state of the control -- for example, to change the splitter regex.

Thanks for helping out here.

Steve

On Jan 21, 2010, at 3:25 PM, Matt Aimonetti wrote:
> When reopening a Cocoa class, you should not overwrite initialize and if you were to do it anyway, don't forget to call super and to return self.
> The Cocoa way is to create your own initializer to end up with something like DuplicateCounterTextField.alloc.initWithDuplicate
> 
> Also, remember that when initializing a Cocoa class, you need to do DuplicateCounterTextField.alloc.init
> 
> - Matt
> 
> 
> On Thu, Jan 21, 2010 at 3:20 PM, steve ross <cwdinfo at gmail.com> wrote:
> I'm sure this is an elementary question, but I have the class below. In IB, I set several NSTextField controls to this class. Everything works in the blah, blah, blah part, but strangely enough the
> 
> puts "initialize dctf"
> 
> never seems to be called. Any thoughts as to why?
> 
> require 'strings'
> 
> class DuplicateCounterTextField < NSTextField
>  include Strings
> 
>  attr_accessor :splitter, :completions
>  attr_accessor :wordCount, :duplicateCount
> 
>  def initialize
>    puts "initialize dctf"
>    @splitter = /\W+/
>    @wordCount = 0
>    @cachedWordCount = 0
>    @duplicateCount = 0
>  end
> 
>  # blah, blah, blah working code
> 
>  def textDidChange(notification)
>    words = stringValue.split(@splitter)
>    @wordCount = words.length
>    @duplicateCount = @wordCount - words.uniq.length
> 
>    if delegate.respond_to?('controlCountDidChange:wordCount:duplicateCount:')
>      delegate.controlCountDidChange(self,
>                                   wordCount:@wordCount,
>                              duplicateCount:@duplicateCount)
>    end
>  end
> 
>  # etc.
> end
> _______________________________________________
> 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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20100121/2df05b5a/attachment.html>


More information about the MacRuby-devel mailing list