subclass method not being called

Discussion in 'iOS Programming' started by flummoxed, Dec 28, 2010.

  1. flummoxed macrumors member

    Nov 27, 2010
    I created a subclass that I want to use as a separate data model apart from the main viewController code. It contains some custom instance variables for the data - just NSIntegers - and a custom instance method to assign values to the variables. In my .h and .m subclass files I am also creating properties of the variables so that I can easily reference them.

    I added this subclassed object as an instance variable to my viewController.
    I then made it a property of the viewController and synthesized it as (nonatomic, retain).

    Then in the viewDidLoad method of the viewController I call the instance method on the subclass to assign values to its variables.

    My problem is that this method does not get run. I know this because I have 2 NSLog messages inside the method and they are not printing in the console.
    Also I am referencing one of the subclass variables in a viewController method and its value remains initialized at 0.

    The build succeeds with no errors and the app runs, but this one method is being ignored.

    Here is what the code looks like...

    the subclass .h and .m files
    @interface payoutData : NSObject {
    	NSInteger ivar; 	
    - (void) assignPayouts;
    @property (nonatomic) NSInteger ivar;
    @implementation payoutData
    @synthesize ivar;
    - (void) assignPayouts {
    		ivar = 5;
           NSLog(@"This message does not get printed\n");		
    the viewController .h and .m code
    @interface My_ViewController : UIViewController {
    payoutData *paydata;
    @property (nonatomic, retain) payoutData *paydata;
    @implementation My_SlotsViewController
    @synthesize  paydata;
    - (void)viewDidLoad {
        [paydata  assignPayouts];
        NSLog(@"the program makes it here because this message gets printed");
    It seems that it could not be any simpler, so why isn't the subclass's method being run?
  2. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    Did you create an instance of paydata? When My_ViewController is initialized, paydata is going to be nil so, unless there is a chunk of code you aren't showing us where you alloc, init and assign paydata, you are sending a message to a nil object.
  3. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Put this in viewDidLoad:
    NSLog( @"paydata is %p", paydata );
    What is the output?
  4. flummoxed thread starter macrumors member

    Nov 27, 2010
    The output of %p is 0x0. I guess meaning no address assignment?

    I'm a bit confused then about how to use this subclass.
    I have other objects declared as instance variables to viewController, but they never are explicitly allocated, unless that happens when the nib is loaded since they are view objects.

    I added an allocation command in viewDidLoad just before the subclass method call..
    paydata = [[payoutData alloc]init];
    [paydata  assignPayouts];
    That works. Could I then just eliminate the assignPayouts method and use the init method to assign my variables?

    Another side of this question is whether I need to use ivars at all ?
    Basically I just want to isolate a bunch of constant values from the viewController code so that I can use them in other places. I just want them in a single location so that I only have to change them once.
    I could just as well define them all as constants in a .h file and never actually instantiate the object. Is that a viable technique?
  5. Svinja macrumors newbie

    Oct 27, 2008
    you can override init method of your payoutData class and assign variables there like this:
    	if(self=[super init])
    		// Put your code here
    	return self;
    i dont recomend doing this if you need only constants, just put them in header file and you are good to go...both ways are good but i prefer not doing unnecessary object creation...Maybe making this with static getters would be most objective-wise solution
  6. flummoxed thread starter macrumors member

    Nov 27, 2010
    Thanks for the help.

    I think constants in a header is the best way in this case.

    But at least I got a better handle on using a subclass.
  7. Sykte macrumors regular

    Aug 26, 2010

    You never instantiated an instance of paydata. So you sent a message to nil, which is completely legal but hard to troubleshoot when you first start out because you do not recieve a warning or error message. In your viewDidLoad trying alloc/initing your payData.

Share This Page