Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

flummoxed

macrumors member
Original poster
Nov 27, 2010
38
0
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
Code:
@interface payoutData : NSObject {
	NSInteger ivar; 	
}
- (void) assignPayouts;

@property (nonatomic) NSInteger ivar;
@end

@implementation payoutData

@synthesize ivar;

- (void) assignPayouts {
		ivar = 5;
       NSLog(@"This message does not get printed\n");		
}	
@end

the viewController .h and .m code
Code:
@interface My_ViewController : UIViewController {
	
payoutData *paydata;
	
@property (nonatomic, retain) payoutData *paydata;
@end

@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?
 
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.
 
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..
Code:
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?
 
you can override init method of your payoutData class and assign variables there like this:
Code:
-(id)init
{
	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
 
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.
 
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.


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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.