Gold Challenge Solution


#1

Created a BNRDateViewController class with XIB. Used IB to put a Date Picker in the middle of the view. Used IB to create a button on the BNRDetailViewController XIB.

BNRDetailViewController.m

One method attached to the new button.

- (IBAction)dateChangeEvent:(UIButton *)sender
{
    BNRDateViewController *dateController = [[BNRDateViewController alloc]init];
    dateController.item = self.item;
    
    [self.navigationController pushViewController:dateController animated:YES];
}

BNRDateViewController.h

Just a property to capture the array.

@class BNRItem;

@interface BNRDateViewController : UIViewController

@property (nonatomic,strong)BNRItem *item;

@end

BNRDateViewController.m

Property to capture the DatePicker on the UI:

#import "BNRDateViewController.h"
#import "BNRItem.h"

@interface BNRDateViewController ()

@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;

@end

Method to set the UI DatePicker to match the date from the item selected when the screen is shown.

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear];
    
    BNRItem *item = self.item;
    
    [self.datePicker setDate:item.dateCreated];
}

Method on the setter to create a navigation title with the original date (note-this could also have been put in ViewWillAppear above).

-(void)setItem:(BNRItem *)item
{
    _item = item;
    
    static NSDateFormatter *dateFormatter = nil;
    if (!dateFormatter)
    {
        dateFormatter = [[NSDateFormatter alloc]init];
        dateFormatter.dateStyle = NSDateFormatterMediumStyle;
        dateFormatter.timeStyle = NSDateFormatterNoStyle;
    }
    
    self.navigationItem.title = [dateFormatter stringFromDate:_item.dateCreated];
}

Final method to populate BNRItem with the new date selected. This is fired from a “Value Changed” event on the DatePicker (note-this could have been put in ViewWillDisappear)

- (IBAction)dateSelect:(UIDatePicker *)sender
{
    self.item.dateCreated = [sender date];
}
@end

#2

I forgot to add one thing here. In BNRItem dateCreated was setup as a ReadOnly property from previous exercises. Since we are now changing this date with this solution it was necessary to remove ReadOnly from dateCreated in BNRItem.h


#3

I did more or less the same as you, although I didn’t add the navigationtitle, which I stole from you :stuck_out_tongue:
I did however add one bit of functionality in my custom IBAction:

- (IBAction)setDate:(id)sender { self.item.dateCreated = self.datePicker.date; [self.navigationController popToViewController:self.dvc animated:YES]; }

The last line pops the nagivationcontroller back to the the previous view controller after you click Done. This does require you to add a property to hold an instance of DetailViewController and you need to pass the current DetailViewController along with the BNRItem when pushing the new viewcontroller.

Also instead of creating a button in the IB, I added a button at the top of the view, in the navigationcontroller, much like we did in the BNRDeatilViewController, like so:

[code]- (void)setItem:(BNRItem *)item
{
_item = item;

static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
}
self.navigationItem.title = [dateFormatter stringFromDate:_item.dateCreated];

// Create a new bar button item that will send didChangeDate: to BNRDateViewController
UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                     target:self
                                                                     action:@selector(didChangeDate:)];

// Set this bar button item as the right item in the navigationItem
self.navigationItem.rightBarButtonItem = bbi;

}
[/code]


#4

Thanks for the posted solution. Did a couple of things different:

1] Adjusted the date picker to the original date in the “viewDidAppear” method so that you could animate the picker going to the original date:

-(void)viewDidAppear:(BOOL)animated { [super viewDidAppear]; CPEItem *item = self.item; [self.datePicker setDate:item.dateCreated animated:YES];

2] Instead of setting the navigation title in a new setter for the DateViewController, I just set it before the previous VC passed control (i.e., this code is part of the DetailViewController):

[code]- (IBAction)changeDate:(id)sender {

CPEChangeDateViewController *dateVC = [[CPEChangeDateViewController alloc] init];

dateVC.item = self.item;

static NSDateFormatter *dateFormatter = nil;
if (!dateFormatter)
{
    dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
}

dateVC.title = [dateFormatter stringFromDate:self.item.dateCreated];

[self.navigationController pushViewController:dateVC animated:YES];

}[/code]