Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > [newbie] Setting Instance Variables at Start Up

[newbie] Setting Instance Variables at Start Up
Thread Tools
xoedusk
Fresh-Faced Recruit
Join Date: Sep 2006
Status: Offline
Reply With Quote
Sep 1, 2006, 02:28 PM
 
For the life of me, I can't figure out how to assign values to all my different instance variables of my objects at startup? Does it have something to do with awakeFromNIB? I'm not even sure how to access awakeFromNib. I am using Xcode's Cocoa.

Thank you!
     
Catfish_Man
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Sep 1, 2006, 03:00 PM
 
you just implement -(void) awakeFromNib in any class that's in a nib, and Cocoa will run it when a nib containing objects of that class is loaded.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 1, 2006, 03:10 PM
 
In general, -awakeFromNib should be used only if you actually need other objects in a nib to do the setup. If you just want to, for example, set your Person class's name to "Brian" by default, that would generally be done in the -init method.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
xoedusk  (op)
Fresh-Faced Recruit
Join Date: Sep 2006
Status: Offline
Reply With Quote
Sep 1, 2006, 08:25 PM
 
Thank you both for your replies.
Originally Posted by Chuckit
In general, -awakeFromNib should be used only if you actually need other objects in a nib to do the setup. If you just want to, for example, set your Person class's name to "Brian" by default, that would generally be done in the -init method.
I'm not sure if I "need" other objects in a nib to do the setup. I have instantiated all the objects I need through Interface Builder, so I don't know how the -init method would come in to play. Three of the objects are of the same class. What do you think the best way to proceed is?

Thank you!
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 1, 2006, 08:35 PM
 
The objects are created by calling -init on them. If they're instantiated in the nib, it's done behind the scenes (rather than explicitly by you). After the entire nib has been unarchived, -awakeFromNib is called. Before awakeFromNib, none of your IBOutlets will be connected, so you can't communicate with other objects in the nib.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
xoedusk  (op)
Fresh-Faced Recruit
Join Date: Sep 2006
Status: Offline
Reply With Quote
Sep 1, 2006, 08:51 PM
 
Thank you very much. I had my controller object use each of the objects' accessor methods to set up the instance variables. Seems to have complied without problems. Thank you again.
Originally Posted by Chuckit
The objects are created by calling -init on them. If they're instantiated in the nib, it's done behind the scenes (rather than explicitly by you).
Where in the nib file is this -init code hidden?
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 1, 2006, 08:54 PM
 
It's not exactly in the file. It's done by the part of the Cocoa framework that handles nib files.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Gametes
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Sep 4, 2006, 11:41 AM
 
If you are having your controller access those objects in its awakeFromNib: method, it's definitely important to understand that IB doesn't guarantee the order in which objects from your nib are "woken up", which means that Controller might be awakeFromNib:ed before the other objects even exist.
Basically, I think their are two current best practices regarding setting default values.
1 - if the values are for the object independent of anything else going on in your program, set them up in -(id)init; ... you need to set up most global variables that way anyway, EVEN IF you go on to step 2 Ex:

@implementation myClass : NSObject
{
NSMutableString *myString;
}
@end

@interface myClass

-(id)init
{
self = [super init];

myString = [[NSMutableString alloc] int];

return self;
}

@end
2 - If your object needs to get values from another object, or set values of that object, then you need to be sure that everything has been init, everything has had it's connection set up, and all that. Basically that the program is finished launching, so you can set up the default vars. Hence (same class as above, in the implmentation):

- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationDidFinishLaunching
name:NSApplicationDidFinishLaunchingNotification
object:nil];
}

- (void)applicationDidFinishLaunchingNSNotificatio n *)aNotification
{
[myString setString:[controller defaultStringForMyStringObjects]];
}
This is a two-step process that I have used many times for objects which need to inform each other at program launch. Note that this is only for objects which appear somewhere in IB. Any object who is instantiated in your code never gets awakeFromNib.
you are not your signature
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 4, 2006, 03:01 PM
 
Originally Posted by Gametes
If you are having your controller access those objects in its awakeFromNib: method, it's definitely important to understand that IB doesn't guarantee the order in which objects from your nib are "woken up", which means that Controller might be awakeFromNib:ed before the other objects even exist.
This is isn't exactly right, both in my experience and in Apple's documentation. Quoth the awakeFromNib docs: "Messages to other objects can be sent safely from within awakeFromNib—by which time it’s assured that all the objects are unarchived and initialized (though not necessarily awakened, of course)." So other objects in the nib are guaranteed to be there.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Gametes
Mac Elite
Join Date: Sep 2000
Location: Norfolk, Va
Status: Offline
Reply With Quote
Sep 4, 2006, 06:42 PM
 
Yeah, I almost exclusively use init for programmatically created objects and awakeFromNib for IB ones. But the notification does come in handy when you need to be sure everything is up and you want to do finishing touches, like setting a status indicator to "Ready". :thumbsup
you are not your signature
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:26 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,