Gold Challenge Solution


#1

I created a DatePickerController. I added a button to the DetailViewController that responds by pushing a DatePickerController onto the NavigationController’s stack.

DatePickerController.h

#import <UIKit/UIKit.h>

typedef void (^CompletionCallback)(NSDate* inNewDate);

@interface DatePickerController : UIViewController

- (instancetype) initWithDate:(NSDate*)inDate completionCallback:inCallback;

@end

DatePickerController.m

//
//  DatedPickerController.m
//  Homepwner
//
//  Created by Robert Vaessen on TueFeb032015.
//  Copyright (c) 2015 Robert Vaessen. All rights reserved.
//

#import "DatePickerController.h"

@implementation DatePickerController {
	CompletionCallback	mCallback;
}

- (instancetype) init {
	return [self initWithDate:[NSDate date] completionCallback:nil];
}

- (instancetype) initWithDate:(NSDate*)inDate completionCallback:(id)inCallback {
	self = [super init];
	if (self) {
		UIDatePicker* thePicker = [[UIDatePicker alloc] init];
		thePicker.date = inDate;
		self.view = thePicker;

		mCallback = inCallback;
		
		[self logDate:thePicker.date usingDescription:@"started with date = "];
	}
	return self;
}

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

	UIDatePicker* thePicker = (UIDatePicker*)self.view;

	if(mCallback) mCallback(thePicker.date);

	[self logDate:thePicker.date usingDescription:@"finished with date = "];
}

- (void) logDate:(NSDate*)inDate usingDescription:(NSString*)inDescription {
	NSString* theLocalDate = [NSDateFormatter localizedStringFromDate:inDate dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];
	NSLog(@"%@: %@ %@", NSStringFromClass([DatePickerController class]), inDescription, theLocalDate);
}

@end

DetailViewController’s IBAction

- (IBAction) changeDate:(id)inSender {
	DatePickerController* theDatePicker = [[DatePickerController alloc] initWithDate:_item.dateCreated completionCallback:^void (NSDate* inDate) { self.item.dateCreated = inDate; }];
	[[self navigationController] pushViewController:theDatePicker animated:YES];
}