Gold challenge: NSDate object as DateViewController's model


#1

Hi all,
I’m taking the approach to use an NSDate object as the DateViewController’s model but things don’t go as expected.

This is the action code for Change Date Button in DetailViewController.m:

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

DateViewController *dvc=[[DateViewController alloc] init];
[dvc setDateCreated:[item dateCreated]];
[[self navigationController] pushViewController:dvc animated:YES];

}[/code]

This is DateViewController.h

[code]#import <UIKit/UIKit.h>
@interface DateViewController : UIViewController
{
__weak IBOutlet UIDatePicker *datePicker;
}

@property (nonatomic,strong) NSDate *dateCreated;
@end[/code]

and this is the code that handles data saving in DateViewController.m

-(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear]; //[[self view] endEditing:YES]; //NSLog(@"%@",datePicker.date); dateCreated=datePicker.date; }

When I return back to the DetailViewController, the date is not updated.
Do you see any reason for this?


#2

I didn’t try it, but It seems you never passed the updated date back to the detailViewController. dateCreated is local to you dateViewController. You updated that in viewWillDissapear. Once the view disappears, nothing references this updated date, and thus it gets destroyed.


#3

Ok now its more clear:

the statement
dateCreated=datePicker.date;

does not change the contents of the object dateCreated pointer points to. It just changes the value of dateCreated pointer, to point to the object datePicker.date.
If something like this was possible:
[dateCreated setDate:datePicker.date];
then the date would be updated


#4

I also designed this way initially and of course it doesn’t work. After some research, I found this is the Apple documentation:

“The objects you create using NSDate are referred to as date objects. They are immutable objects.”

That’s why the change doesn’t pass back to DetailViewController.


#5

If you look at BNRItem.h

@property (nonatomic, readonly, strong) NSDate *dateCreated; @property (nonatomic, copy) NSString *itemName;
Is how the dateCreated was declared, but also note how itemName was created by using copy. If you declare NSDate in the same manner you can use a code such as

[item setDateCreated:[datePickerVariable date]];


#6

It seems you forgot to pass the pointer of item from DetailViewController to datePickerViewController, so the BNRItem instance never get chance to get initialised, so all its values are nil.

when you send nil messages, nothing happens.

add code in your changeDate: method

[dvc setItem:item];

so each time you go to the next level, its controller knows which item you are talking about.

Hope it helps.


#7

[quote=“dirtybit”]Hi all,
I’m taking the approach to use an NSDate object as the DateViewController’s model but things don’t go as expected.

This is the action code for Change Date Button in DetailViewController.m:

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

DateViewController *dvc=[[DateViewController alloc] init];
[dvc setDateCreated:[item dateCreated]];
[[self navigationController] pushViewController:dvc animated:YES];

}[/code]

This is DateViewController.h

[code]#import <UIKit/UIKit.h>
@interface DateViewController : UIViewController
{
__weak IBOutlet UIDatePicker *datePicker;
}

@property (nonatomic,strong) NSDate *dateCreated;
@end[/code]

and this is the code that handles data saving in DateViewController.m

-(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear]; //[[self view] endEditing:YES]; //NSLog(@"%@",datePicker.date); dateCreated=datePicker.date; }

When I return back to the DetailViewController, the date is not updated.
Do you see any reason for this?[/quote]

I have exactly the same issue - can someone please explain this?


#8

In order to change the dateCreated value in BNRItem, there needs to be a way to pass the data between the view controllers. The method I used is described on page 231, under “Passing data between view controllers.”

I added an item property to my DateViewController to hold a BNRitem. This is the same method used in the DetailViewController class in the book. This seemed to resolve the issue of transferring the selected item data between all view controllers: ItemsViewcontroller, DetailedViewController, and DateViewController.

Here’s the code listing:


//
//  DateViewController.h
//  Homepwner
//

#import <UIKit/UIKit.h>
@class BNRItem;

@interface DateViewController : UIViewController
{
    __weak IBOutlet UIDatePicker *datePicker;
}

@property (nonatomic, strong) BNRItem *item;

@end


//
//  DateViewController.m
//  Homepwner
//

#import "DateViewController.h"
#import "BNRItem.h"

@interface DateViewController ()

@end

@implementation DateViewController
@synthesize item;

- (void)viewWillAppear:(BOOL)animated  
{
    [super viewWillAppear];
    [datePicker setDate:[item dateCreated] animated:YES]; 
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear];
    [item setDateCreated:[datePicker date]];
    
}

- (void)viewDidUnload {
    datePicker = nil;
    [super viewDidUnload];
}
@end