Gold solution question- DetailViewController not redrawing


#1

So I think I have figured out a solution, but the one thing that is not working is that the detail view does not redraw after selecting a date and returning to the detail view. However, if I tap back on the change date button or return to the items view and return to the detail view, the date I selected is displayed. I tried adding [self.view setNeedsDisplay] in viewWillAppear in BNRDetailViewController.m but that did not make a difference. I am stumped. Any help would be appreciated. Here’s my code:

BNRDatePickerController.h

[code]#import <UIKit/UIKit.h>

@class BNRItem;

@interface BNRDatePickerViewController : UIViewController

@property (strong, nonatomic) BNRItem *item;

@end
[/code]

BNRDatePickerViewController.m

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

@interface BNRDatePickerViewController ()
@property (weak, nonatomic) IBOutlet UIDatePicker *selectDate;

@end

@implementation BNRDatePickerViewController

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

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    }

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear];

BNRItem *item = self.item;

[self.selectDate setDate:item.dateCreated];

}

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

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

    // “Save” changes to item
    BNRItem *item = self.item;
    item.dateCreated = self.selectDate.date;
    }

@end[/code]

BNRDetailViewController.m

[code]#import “BNRDetailViewController.h”
#import “BNRItem.h”
#import “BNRDatePickerViewController.h”

@interface BNRDetailViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *serialNumberField;
@property (weak, nonatomic) IBOutlet UITextField *valueField;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;
@property (weak, nonatomic) IBOutlet UIButton *changeDate;

  • (IBAction)changeDate:(id)sender;

@end

@implementation BNRDetailViewController

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

    BNRItem *item = self.item;

    self.nameField.text = item.itemName;
    self.serialNumberField.text = item.serialNumber;
    self.valueField.text = [NSString stringWithFormat:@"%d", item.valueInDollars];

    // You need a NSDateFormatter that will turn a date into a simple date string
    static NSDateFormatter *dateFormatter = nil;
    if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
    }

    // Use filtered NSDate object to set dateLabel contents
    self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];
    }

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

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

    // “Save” changes to item
    BNRItem *item = self.item;
    item.itemName = self.nameField.text;
    item.serialNumber = self.serialNumberField.text;
    item.valueInDollars = [self.valueField.text intValue];
    }

  • (void)setItem:(BNRItem *)item
    {
    _item = item;
    self.navigationItem.title = _item.itemName;
    }

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nameField resignFirstResponder];
[self.serialNumberField resignFirstResponder];
[self.valueField resignFirstResponder];
}

  • (IBAction)changeDate:(id)sender {
    BNRDatePickerViewController *datePickerViewController = [[BNRDatePickerViewController alloc] init];

    // Give detail view controller a pointer to the item object in row
    datePickerViewController.item = self.item;

    [self.navigationController pushViewController:datePickerViewController
    animated:YES];
    }

@end[/code]


#2

You may have already figured this out, but I just came across the same issue you were having. It turns out that you (and I) called viewDidDisappear rather than viewWillDisappear in BNRDatePickerViewController.m.

This solved my problem and I hope it’ll solve yours! That is if you haven’t already fixed it.

Cheers.


#3

I got so frustrated that I moved on and was going to come back to it after my head cleared. That solved the issue for me too. Thanks!