small medium large xlarge

Generic-user-small
21 Jan 2011, 14:54
Ric Levy (21 posts)

I am working on the shopping list app in chapter 11, and I thought I would try to pre-populate the Quantity field so it says “1” as soon as you start the app.

I put the following line of code in the init method:

-(id)init 
{
    self = [super init];
    if (self) 
	{
		// other stuff

		[newItemQuantityTextField setStringValue:@"1"];
	}
    return self;
}

</code>

Why does this have no effect?

Generic-user-small
21 Jan 2011, 15:05
Tim Isted (105 posts)

Hi Ric,

At the time the init method is called, the objects in the nib/xib haven’t yet been loaded. During this init method, the nib-owning object itself is still being initialized, so it hasn’t yet gone about loading the “freeze-dried” objects from the nib.

To perform post-nib-loading setup work like this, you need to use a method that is called after the nib/xib objects have been loaded; the awakeFromNib method is specifically designed for this purpose.

There’s more information about this later in the book; look for the sidebar titled “Initializing Objects”, in Chapter 13, which is on p. 327 of Printing 1.0 paper copy, otherwise look for “awakeFromNib” in the book’s index.

Tim

Generic-user-small
21 Jan 2011, 15:14
Ric Levy (21 posts)

Thanks for that amazingly quick reply! I just did this …

- (void)awakeFromNib
{
	[newItemQuantityTextField setStringValue:@"1"];
}

</code> … and it worked!

Am I right in saying that, as it’s part of a protocol, this method does not have to be declared in the header?

You must be logged in to comment