I have a method that I am using recursively. I have 2 variables I am using to carry data between the recursive calls. Here is a example of what I am trying to do...
Why does the NSString EditType work fine while the NSDate newDate gets deallocated between calls to the recursive method?
All I need to do is retain newDate after i set it, but I do not understand why is it getting deallocated. I am not touching it anyplace else.
Thanks for any help.
John
Code:
// TripsViewController.h
@interface TripsViewController : UIViewController {
NSString *editType;
NSDate *newDate;
}
@property (nonatomic, retain)NSString *editType;
@property (nonatomic, retain)NSDate *newDate;
- (void)editWith:(NSDate *)pickedDate location:(NSString *)pickedLocation;
- (void)datePickerIsDone;
- (void)locationSearchViewDidDismisswithLocation:(NSString*)location;
@end
Code:
// TripsViewController.m
@implementation TripsViewController
@synthesize editType, newDate;
- (void)viewDidLoad {
[super viewDidLoad];
editType = @"";
newDate = [[[NSDate alloc] dateWithTimeIntervalSinceReferenceDate:0] retain];
}
- (void)editWith:(NSDate *)pickedDate location:(NSString *)pickedLocation{
if([editType isEqualToString:@"Add"]){
editType = @"Add1";
[self pickDate]; //pick date method invokes a UIDatePicker with a done button callback to datPickerIsDone
{else if ([editType isEqualToString:@"Add1"]) {
editType = @"Add2";
newDate = [df dateFromString:[df stringFromDate:nsDate]]; //this gives me an NSDate at midnight. df is set to short date and no time
NSLog(@"newDate is set properly here: %@", newDate);
LocationSearchViewController *locationSearchViewController = ...etc.;
locationSearchViewController.delegate = self;
[self presentModalViewController:locationSearchViewController animated:YES];
}else if ([editType isEqualToString:@"Add2"]) {
NSLog(@"newDate is dealocated and this line will throw an exception: %@", newDate);
}
}
- (void)datePickerIsDone
{
NSDate *datePicked = datePickerView.date;
[self closeDatePicker];
[self editWith:datePicked location:nil];
}
- (void)locationSearchViewDidDismisswithLocation:(NSString*)location {
[self dismissModalViewControllerAnimated:YES];
[self editWith:nil location:location];
}
@end
Why does the NSString EditType work fine while the NSDate newDate gets deallocated between calls to the recursive method?
All I need to do is retain newDate after i set it, but I do not understand why is it getting deallocated. I am not touching it anyplace else.
Thanks for any help.
John