I have a model.h and model.m to keep my data and variables which various view controllers access for data(Using MVC). The problem is, When i try to set and get the variables from them, They are always 0. I’m obviously done something wrong, but have no idea.
Here is my model.h and model.m (changed names so its easier.
model.h
@interface modeler : NSObject
{
int abc;
int def;
}
@property (nonatomic,assign) int abc;
@property (nonatomic,assign) int def;
model.m
#import "modeler.h"
@interface modeler()
@end
@implementation modeler
@synthesize abc = _abc;
@synthesize def = _def;
Thats how i set up the two variables, here are the view controllers.
vc.h
#import "modeler.h"
@interface vcViewController : UITableViewController
@property (nonatomic,strong) modeler *datas;
@end
vc.m
@interface vcViewController ()
@end
@implementation vcViewController
@synthesize datas = _datas;
This is how im trying to set them to something in vc.m
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
NSLog(@"1-%i",selectedRowIndex.row);
_datas.abc = selectedRowIndex.row;
NSLog(@"2-%i",_datas.abc);
The selected row prints out 2, but the _data.abc is always 0. What did i miss? I am guessing its not calling it, maybe because its an instance of it ? Any quick tips?
I don’t see where you’re setting the view controller’s
datasproperty (or the_datasivar) to an instance of yourmodelerclass. If that’s not happening,_datas.abcis a message to nil — so using it as a setter does nothing and using it as a getter returns 0.In addition, you’re declaring
abcanddefas ivars on yourmodelerclass, and then declaring properties with the same names but different ivars (_abcand_def). This almost certainly isn’t causing the problem you’re seeing, but is bound to lead to confusion later. It’d be better to either not declare ivars (and let the@synthesizedirectives take care of that) or not specify ivars in your@synthesizedirectives (so that the same-named ivars are used by default. I tend to prefer the former.Furthermore, it’s also probably not connected to your problem, but it’s generally a good thing to use generated accessors instead of ivars unless you have a good reason not to. (That is, use
self.datasinstead ofdatas.)