0

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];

}

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

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

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

self.today = [NSDate date];

2 Comments

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.
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.
0

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

Comments

-1

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

@synthesize today;

1 Comment

It wouldn't compile if he didn't. Is the today ivar ever assigned to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.