DetailedViewController Label not updating the date


#1

I saw where others have struggled to change the dateLabel in the DetailedViewController and I am having the same issue.

//DatePickerController

[code]#import “DatePickerController.h”
#import “BNRItem.h”

@interface DatePickerController ()

@end

@implementation DatePickerController
@synthesize item;

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    [myDatePicker setDatePickerMode:(UIDatePickerModeDate)];
    // Do any additional setup after loading the view from its nib.
    NSLog(@“current date is %@”, [myDatePicker date]);
    }

  • (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear];

}

-(void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear];
//[[self view] endEditing:YES];

[item setDateCreated:[myDatePicker date]];

NSLog(@"changed date is %@", [myDatePicker date]);

}
@end[/code]

Like we did with the DetailedViewController and the ItemsViewController on the VIewWillDisappear method we set the values of the text in the BNRItem, i did the same here as you can see in the code above.

Then when the DetailedViewController class is the view, its ViewWIllAppear method should have the correct date that you have changed.

[code]…
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

[dateLabel setText:[dateFormatter stringFromDate:[item dateCreated]]];
…[/code]

But, yet, the label doesn’t update to the date that i had changed. It just shows the current date. Not sure what is going on here.

I even thought that i would override the setDateCreated method in BNRItem to see more closely what was going on.

//BNRItem.m

-(void)setDateCreated:(NSDate *)date{ NSLog(@"setting the new date"); dateCreated = date; }

but the log message doesn’t even show up in the console. - like the method was really never called.

I overrode one of the other setter methods in BNRItem that gets called in DetailViewController and that method does return a log statement.

So the next step was to provide a setter method in DetailViewController:

-(void)setNewDate:(NSDate *)newDate{ [item setDateCreated:newDate]; }

and from the DatePickerController call this method:

[code]-(void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear];
//[[self view] endEditing:YES];

DetailViewController *dvc;
dvc = [[[self navigationController] childViewControllers] objectAtIndex:1];
[dvc setNewDate:myDatePicker.date];

NSLog(@"changed date is %@", [myDatePicker date]);

}
[/code]

This seems to work. So my question is - Is how come the DetailedViewController class was able to set the date but in the DatePickerController i was not able to? Remember that the setDateCreated: method seemed to have never been called from my DatePickerController class.

It seems to me that using “objectAtIndex” to get to the controller that you want is pretty bad anyway. not only are you using a constant but you maybe have a thread of viewcontrollers and you might not know ahead of time what index you really want.

Maybe the mods can chime in here? Is there a way for the DatePickerController to communite to BNRItem to set the date instead of going through the DetailedViewController? thanks for the insight in advance.


#2

Try NSLogging your dateLabel when you try setting it… bet you it turns up nil.