views:

1001

answers:

5

Normally i've been passing variable around in init methods, but I can't do that this time because I have a var in one ViewController class displayed using a tab bar and I need access to it from a different ViewController class when a different tab bar is pressed. My understanding was that you can access vars using @property but it's now working so I'm doing something wrong. Here is what I have:

Class 1 Header file

@interface DailyViewController : UIViewController <UIActionSheetDelegate> {

NSDate *today;

}

@property (readwrite, nonatomic, retain) NSDate *today;



Class 2 implementation file:


- (void)viewWillAppear:(BOOL)animated{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
DailyViewController *otherClass = [[DailyViewController alloc] init]; 

NSString* todayString = [formatter stringFromDate:otherClass.today];
r_todayLabel.text = todayString;
[otherClass release];
[formatter release];

}

A: 

In the source file "Class1.m" do you have

@synthesize today;
Ryan Detzel
It wouldn't compile if he didn't. Is the today ivar ever assigned to?
arul
yes, i have @synthesize today;
Xcoder
+3  A: 

Without having

@synthesize today;

in your "Class1.m" file, the getter and setter methods for today are never created. This means that your property cannot be changed or seen from outside.

Dan Lorenc
have @synthesize today;
Xcoder
A: 

You need to initialize today, for example in constructor of DailyViewController add the following:

self.today = [NSDate date];
Valerii Hiora
Yes, it's been initialized.
Xcoder
For clarification. I initialize it in the app delegate and then pass the date to the first class using an initWithDate method. Now I need to pull the date from the first class into the second class.
Xcoder
In the code you've provided you're creating new instance of DailyViewController with simple init method, there is no call of initWithDate and there is no setting date for otherClass instance.So if you're setting it in app delegate with initWithDate method you should take data from that class instance.
Valerii Hiora
+1  A: 

You need to retrieve the DailyViewController* object from your AppDelegate (or wherever it is stored), and retrieve the date from it.

You are creating a new DailyViewController* object, not initializing it with its date, and then accessing its date field (which will be nil by default).

Something like

MyAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];
DailyViewController *otherClass = appDelegate.dailyViewController;
NSDate* dailyViewToday = otherClass.today;

However all that is violating lots of rules of good programming.

Firstly, you should be aiming for MVC (Model View Controller), so your "today" date should be stored in your model. Then both classes could access today from the model rather than from one view's controller and then there would be no need for class 2 to access the DailyViewController at all and so no need to store a reference to it in the AppDelegate, which is a bad idea since it has nothing to do with delegation for the UIApplication and is really just a global variable in disguise.

Peter N Lewis
Yes, I didn't want to make it global by using the delegate but thats what I ended up doing. Thanks Peter.
Xcoder
A: 

In looking at Peter's answer ("Firstly, you should be aiming for MVC (Model View Controller), so your "today" date should be stored in your model. Then both classes could access today from the model rather than from one view's controller and then there would be no need for class 2 to access the DailyViewController at all and so no need to store a reference to it in the AppDelegate, which is a bad idea since it has nothing to do with delegation for the UIApplication and is really just a global variable in disguise."), I have a question.

How do both classes access the Model? Would the init call for each view Controller be passed in a pointer to the model? (ie, the AppDelegate creates the model object, then passes the pointer to each of the view controller's init methods?)

Sal