Gold Challenge, can't update date label


#1

I have been working on this for a while, I reset the date, and the console window reflects this, but the date label in the Detail View doesn’t change. Any suggestions?

[code]#import “DateChangerController.h”
#import “DetailViewController.h”
#import “BNRItem.h”
#import “BNRItemStore.h”

@interface DateChangerController ()

@end

@implementation DateChangerController

@synthesize item;

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    [datePicker setDatePickerMode:(UIDatePickerModeDate)];
    }

  • (void)viewWillDisappear:(BOOL)animated
    {
    [item setDateCreated:[datePicker date]];
    NSLog(@“Date created:%@”, datePicker.date);
    }

  • (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    }

@end[/code]

DetailViewController.m:

[code]#import “DetailViewController.h”
#import “BNRItem.h”
#import “DateChangerController.h”

@implementation DetailViewController

@synthesize item;

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    [[self view] setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    }

  • (void)viewWillAppear:(BOOL)animated

{
[super viewWillAppear];

[nameField setText:[item itemName]];
[serialNumberField setText:[item serialNumber]];
[valueField setText:[NSString stringWithFormat:@"%d", [item valueInDollars]]];

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

[_dateLabel setText:[dateFormatter stringFromDate:[item dateCreated]]];

}

  • (void)setItem:(BNRItem *)i
    {
    item = i;
    [[self navigationItem] setTitle:[item itemName]];
    }

  • (void)changeDate:(id)sender
    {
    DateChangerController *dcc = [[DateChangerController alloc]init];

    [[self navigationController] pushViewController:dcc
    animated:YES];
    }

  • (void)viewWillDisappear:(BOOL)animated
    {
    [super viewWillDisappear];

    // Clear first responder
    [[self view] endEditing:YES];

    [item setItemName:[nameField text]];
    [item setSerialNumber:[serialNumberField text]];
    [item setValueInDollars:[[valueField text] intValue]];
    }

@end
[/code]


#2

I had the same problem. I think the detail view was cached in the memory. I added this to -(void)viewDidLoad in DetailViewController.m

[[self view] setNeedsDisplay];

that forces the view to reload and it works.


#3

Alternatively, you can assume that the date value has changed after running the DateChangerController and update only the label after it has exited. So in DetailViewController.m

[code]- (void)changeDate:(id)sender
{
DateChangerController *dcc = [[DateChangerController alloc]init];

[[self navigationController] pushViewController:dcc
                                       animated:YES];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];   
[_dateLabel setText:[dateFormatter stringFromDate:[item dateCreated]]];

}
[/code]

I’m curious as to whether this might also work since UILabel inherits from UIView to refresh just the label, instead of refreshing the entire view.


#4

I had the same problem, then I realised I forgot to pass the current BNRItem pointer to the next level controller.

- (void)changeDate:(id)sender
{
    DateChangerController *dcc = [[DateChangerController alloc]init];
    
    [[self navigationController] pushViewController:dcc
                                           animated:YES];
    
    // **added code here***
    [self.navigationController setItem:item];

}