开发者

TableView - iVar reset when returning from details view

开发者 https://www.devze.com 2022-12-28 03:03 出处:网络
anyone knows what I need to do to retain my TableView iVars whilst pushing a details view onto the navigation stack?

anyone knows what I need to do to retain my TableView iVars whilst pushing a details view onto the navigation stack?

I have an array and a date defined as iVars and the array is retained, whilst the date is not. I checked whether there may be an autorelease hidden somewhere but there are no obvious ones.

The properties are defined as nonatomic, retain.

I use custom NSDate category methods to determine specific dates at stages. These use NSDateComponents, NSRange and NSCalendar, for example:

- (NSDate *)lastDayOfMonth: {
 NSCalendar *tmpCal = [NSCalendar currentCalendar];
 NSDateComponents *tmpDateComponents = [tmpCal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSEraCalendarUnit | NSWeekCale开发者_开发技巧ndarUnit | NSWeekdayOrdinalCalendarUnit fromDate:self];
 NSRange tmpRange = [tmpCal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[tmpCal dateFromComponents:tmpDateComponents]];

 [tmpDateComponents setDay:tmpRange.length];
 [tmpDateComponents setHour:23];
 [tmpDateComponents setMinute:59];
 [tmpDateComponents setSecond:59];
 return [[NSCalendar currentCalendar] dateFromComponents:tmpDateComponents];
}

could they somehow be the reason?


dateFromComponents: returns an autoreleased NSDate.

Your date ivar should be retained by the setter. Did you declare it like this and use the setter?

@property (nonatomic, retain) NSDate* myDate;


  • Do you @synthesize the date accessor, or write it yourself? If you wrote the setter yourself, make sure it calls retain (I've forgotten to do that sometimes)
  • Does the pushed view controller have access to it, and release it accidentally?


I think I found it, when I call the subview, I use a method to generate the view and pass it the data object.

If I figure it right, the parameter is released by the subview when it returns to the main routine and that's why the dates get lost. A retain on the Object in the subview call does the job.

0

精彩评论

暂无评论...
验证码 换一张
取 消