Class Created in Interface Builder being dealloc too soon

Discussion in 'iOS Programming' started by jsasitorn, May 30, 2010.

  1. jsasitorn macrumors newbie

    Joined:
    May 30, 2010
    #1
    Hi, I was following a tutorial on developing a simple application involving Buttons and a text field in Cocoa. The original tutorial is here: http://www.informit.com/articles/article.aspx?p=336259.

    After completing this tutorial I attempted to convert my simple Cocoa application over to an iPhone application. However, the problem that I am having is that the class and its related instance that I created in InterfaceBuilder (with my new class named Engine being a subclass of NSObject) is being dealloc-ed immediately after it is created.

    I saw that someone had a similar problem here:
    http://stackoverflow.com/questions/2612484/object-created-in-interface-builder-getting-dealloced-too-soon, but since I created both the class and instance in Interface Builder, I don't see any place to add an assign property.

    I have added the fields and actions directly to my View class with success. But, I was hoping to follow the same model as my Cocoa application, and create my Engine class in Interface Builder.

    To summarize, if I create an instance of a class in Interface Builder for an iPhone Application, how do I prevent it from being dealloc-ed before it receives its Actions?

    Thanks,
    James
     
  2. TiberiusXavier macrumors member

    Joined:
    Apr 18, 2010
    Location:
    Chicago
    #2
    It would be best if you supplied how you used the Engine object. Either post the code of your Engine .h and .m files or how your view controller is encapsulating or referencing the engine.

    Going through a 5 page tutorial to find out how you either made an error or how the tutorial failed to articulate an aspect is very labor intensive.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    There are differences in how outlets are managed between MacOS X cocoa and iPhone OS UIKit. On UIKit you need to retain all top level objects from your nib. This is almost always done by having retaining properties for your IBOutlets.

    If you're not doing this you should show your header file that declares the outlets.
     
  4. jsasitorn thread starter macrumors newbie

    Joined:
    May 30, 2010
    #4
    @TiberiusXavier: Sorry that I did not provide the files. Here are the two files created through Interface Builder and completed in Xcode. The Delegate and Controller (generated as part of the iPhone Application) are unchanged. Engine is instantiated through Interface Builder. All the connections are done through Interface Builder as well.
    ----------- Engine.h ----------------
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>

    @interface Engine : NSObject {
    IBOutlet UILabel *textField;
    }
    @property(nonatomic,retain) IBOutlet UILabel * textField;

    - (IBAction)generate: (id)sender;
    - (IBAction)seed: (id)sender;


    @end
    ---------------------------------------

    -------------Engine.m---------------
    #import "Engine.h"

    @implementation Engine
    @synthesize textField;

    - (IBAction)generate: (id)sender {
    }

    - (IBAction)seed: (id)sender {

    }
    @end
    --------------------------------------

    @PhoneyDeveloper: Thats what it seemed like, but wasn't sure if this was what should be expected. However, I'm not sure what the correct way to put the retaining properties. In the example I am testing, I have a button that is connected to my Engine class. When the button is clicked, it calls the generate method. At the point this is called, the Engine class instantiation (created in Interface Builder) is already dealloc-ed. So I'm not sure what/where/and for what I need to set additional properties.

    As an aside, if I instantiate the Engine class through Xcode, I have a variable (field or global) that I can explicitly alloc and de-alloc through my Controller class. This works. But, I would like to know if I instantiated a class through Interface Builder, what I would need to do to get it to be retained.

    Let me know if I need to provide any additional info. Thanks again.

    James
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    If you are creating the Engine object in IB then that object must be an IBOutlet also. Typically that would be done in the view controller, which is the File's Owner.

    The retaining property you show looks correct. The only thing is that it's best to only have the IBOutlet token on the @property, not on the ivar.
     
  6. jsasitorn thread starter macrumors newbie

    Joined:
    May 30, 2010
    #6
    Awesome... That solves my problem. The mechanisms linking develop in Xcode and Interface Builder are being to make much more sense to me.

    Thanks for the explanation and tips.

    Regards,
    James
     

Share This Page