Gold Challenge Solution w/ Screenshot


#1

Here is my solution with screenshot. The code below just shows code needed for Gold Challenge.

Feedback ALWAYS welcomed. :slight_smile:

BNRItem.h

#import <Foundation/Foundation.h>
 
// Gold Challenge set Date.
@property NSDate *dateCreated;

@end

DetailViewController.h (I used the nifty trick on page 227 to create the IBOutlet and IBAction)

#import <UIKit/UIKit.h>

@class BNRItem;

@interface DetailViewController : UIViewController
{
    __weak IBOutlet UIButton *changeDate;
}
@property (nonatomic, strong) BNRItem *item;

- (IBAction)changeDate:(id)sender;

@end

DetailViewController.m

#import "DateViewController.h"

- (IBAction)changeDate:(id)sender
{
    // Create a new Date View Controller instance
    DateViewController *dvc = [[DateViewController alloc] init];
    
    // Set the dateItem property to the current BNRItem *item
    // This is how we pass it to the dvc controller
    [dvc setDateItem:item];
    
    // Push this new Date View Controller onto the Navigation stack
    [[self navigationController] pushViewController:dvc
                                           animated:YES];
}

DateViewController.h – New View Controller to handle picker

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

@class BNRItem;

@interface DateViewController : UIViewController
{
__weak IBOutlet UILabel *dateLabel;
__weak IBOutlet UIDatePicker *myPicker;
}

// This is where we set the passed item from DetailViewController.m
@property (nonatomic, strong) BNRItem *dateItem;

@end[/code]

DateViewController.m

[code]

#import “DateViewController.h”
#import “BNRItem.h”
#import “DetailViewController.h”

@interface DateViewController ()

@end

@implementation DateViewController

@synthesize dateItem;

// EXTRA wanted to display the current date that is associated with BNRItem passed

  • (void)viewWillAppear:(BOOL)animated
    {
    // Create a NSDateFormatter that will turn a date into a simple date string
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    // Use filtered NSDate object to set dateLabel Contents
    [dateLabel setText:[dateFormatter stringFromDate:[dateItem dateCreated]]];
    }

// Save Date selected

  • (void)viewWillDisappear:(BOOL)animated
    {
    [super viewWillDisappear];
    // Get the picker date and save it
    [dateItem setDateCreated:[myPicker date]];
    }

@end[/code]


#2

Overall looks similar to my solution.

One thing you might want to add is to update your UILabel if the UIDatePicker date changes.

Otherwise there may have been an #import that wasn’t necessary and you really didn’t need to declare your “change date” button to an IBOutlet. That aside, looks great!