Object question

Discussion in 'Mac Programming' started by larswik, Sep 10, 2011.

  1. larswik macrumors 68000

    Sep 8, 2006
    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.


    Attached Files:

  2. ViviUO macrumors 6502

    Jul 4, 2009
    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.
  3. admanimal macrumors 68040

    Apr 22, 2005
    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.
  4. larswik thread starter macrumors 68000

    Sep 8, 2006
    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.



    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;
        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];
        [theFirstObject release];
        [theSecondObject release];
  5. gnasher729 macrumors P6


    Nov 25, 2005
    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.
  6. larswik thread starter macrumors 68000

    Sep 8, 2006
    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

Share This Page