PDA

View Full Version : Object question




larswik
Sep 11, 2011, 12:26 AM
Tonight I went back to the basics to better understand how things work together. The goal was to enter 2 numbers into 2 text fields. 1 number would go and be stored in an int value in firstObject and the second number would be stored in secondObject int value. Then when I press the Total button it retrieves the 2 integer values and displays the addition result. Simple, I thought.

I have not set up releases yet for the objects, just FYI. I am dealing with a scope problem it seems. I instantiate the objects in my -(void)awakeFromNib; but the code in the buttons can not see the objects instance variables that I instantiated.

I thought objects were global and methods could send messages to them regardless it's scope. I included the zip file.

Thanks



ViviUO
Sep 11, 2011, 01:14 AM
The first thing I noticed is that you are alloc/init'ing the objects in a different method than where they are being used, which won't work for what you are trying to do specifically.

I also don't see any properties set for your text fields.

I'll give it a go in a little bit and get back to you, if someone doesn't beat me to it.

admanimal
Sep 11, 2011, 01:42 AM
The first thing I noticed is that you are alloc/init'ing the objects in a different method than where they are being used, which won't work for what you are trying to do specifically.


The problem is not that the two objects are being allocated in a different method than where they are used; the problem is that they are declared in a way that limits their scope to only one method. theFirstObject and theSecondObject are only valid inside of awakeFromNib, because that is where they are declared. This is true of any variable (object or primitive) in pretty much every C-like language. Just make them instance variables and they will be valid in all of the instance methods.

larswik
Sep 11, 2011, 01:58 AM
ahhh, I see. Instantiate them in the instance variable area. This is a smaller test project that I wanted to do as I work towards my larger goal. Trying to instantiate an object that will load images and text from the web. I want to load the images & text and store them for 24 hours and not have to reload the files every time a button is clicked.

I am getting a better understanding of how objects work since it is my weak area.

Thanks

----------

It WORKS! thanks. Now that I declare them them as instance variables and instantiate them in the awakeFromNib I can keep the objects around until the program deallocs them.


@implementation test_object_messagingAppDelegate


@synthesize window;

-(void)awakeFromNib{
theFirstObject = [[FirstObject alloc]init];
theSecondObject = [[SecondObject alloc]init];

}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

}

- (IBAction)oneButton:(id)sender {

theFirstObject.number = [oneTextField intValue];
}

- (IBAction)twoButton:(id)sender {

theSecondObject.numberTwo = [twoTextField intValue];
}

- (IBAction)totalButton:(id)sender {
total = theFirstObject.number + theSecondObject.numberTwo;
[totalTextField setIntValue:total];
}
-(void)dealloc{
[theFirstObject release];
[theSecondObject release];

}
@end

gnasher729
Sep 11, 2011, 04:08 PM
-(void)awakeFromNib{
theFirstObject = [[FirstObject alloc]init];
theSecondObject = [[SecondObject alloc]init];

}

It looks very much like awakeFromNib is the wrong place for this.

awakeFromNib is for doing things that you would really want to do in the .nib file, but can't because of some limitations of .nib files. It looks to me as if you would always want these two objects, even if you were to create a test_object_messagingAppDelegate object with alloc/init with no nib file involved at all. So this might fit better into the init method.

larswik
Sep 11, 2011, 04:52 PM
I have been pretty much treating my awakeFromNib's in all my projects as an area where I will do / create the things that my project needs. I assigning values to variables, loading plists from a directory and creating NSMutableDictonary, Arrays and so on.

I am watching the Stanford University videos to get a better understanding of MVC and how they build projects.

When I start a project I have an XIB and an AppDelegate created for me. The AppDelegate creates the NSWindow. This is my V in MVC

I then put buttons, textFields, labels and so on in the XIB.
Then create a AppController class which I link all of these buttons and object to. This would be my C, or Controller in MVC.

Now my M for Model. These are other Classes I use like a DieRolling Class which my AppController would instatiate and use. But you can have many Models that work within your AppController that do different things, right? That is how I am understanding how things work together right now.

Thanks gnasher729