PDA

View Full Version : (id)init as constructor or how to init values?




Sergio10
Mar 30, 2009, 01:54 PM
Hi all,

I need to init position value. This value uses by edit box control via NSObject controller. So I wrote:
- (id) init
{
[position setStringValue: @"My value"];
return self;
}

But it don't works (: Why "position" don't initializes? How to solve this problem?

Thanks,
Sergio



Catfish_Man
Mar 30, 2009, 02:57 PM
You probably want to use -awakeFromNib: rather than -init. Also, you should read up on how to write a correct -init method. http://www.mikeash.com/?page=pyblog/the-how-and-why-of-cocoa-initializers.html covers it in some detail.

Sergio10
Mar 31, 2009, 10:07 AM
Thanks for answering. It's work now.
If -(void)awakeFromNib responsible for outlets, value initializing, so why we should to use -(id)init function?

Catfish_Man
Mar 31, 2009, 01:48 PM
Most objects are not created from nibs, so -awakeFromNib: is never called on them.