我的getter和设置有问题,他们没有被调用。我以为我明白他们是如何工作的,但看起来我有问题...
我知道我在使用select按钮之前没有初始化DateRangeController视图,但是知道它的nil我可以在viewDidLoad中设置值。如果我必须初始化它如何设置nib文件等?
这是我的父屏幕,我在其中设置值并获取值
@interface PieChartViewController : UIViewController {
DateRangeController *nextController;
}
@property (nonatomic, retain) DateRangeController *nextController;
@implementation PieChartViewController
@synthesize nextController;
-(void)viewWillAppear:(BOOL)animated {
if ([nextController StartDate] == nil) {
[nextController setStartDate:[NSDate date]];
}
// DBStartDate returns nil and yes convertNSDateToDBStringDate works !
NSString* DBStartDate = [General convertNSDateToDBStringDate:
[nextController StartDate]];
}
- (void) selectRangeButtonPressed {
nextController = [[[DateRangeController alloc]
initWithNibName:@"DateRange" bundle:nil] autorelease];
nextController.title = @"Date Range";
}
继承我的选择/儿童屏幕。
@interface DateRangeController : UIViewController {
NSDate *returnStartDate;
}
-(NSDate*) StartDate;
-(void)setStartDate:(NSDate*) value;
@end
@implementation DateRangeController
-(NSDate*) StartDate {
return returnStartDate;
}
-(void)setStartDate:(NSDate*) value {
if (value != returnStartDate) {
[value retain];
[returnStartDate release];
returnStartDate = value;
NSLog(@"StartDate=%@", returnStartDate);
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)
nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
returnStartDate = nil;
}
return self;
}
- (void)dealloc {
[returnStartDate release];
}
答案 0 :(得分:0)
尝试将“value”与isEqualToDate - 函数进行比较,而不是“!=” - 运算符:
if(![value isEqualToDate: returnStartDate]) { ...}
编辑:如果你需要nextController将startDate存储在viewWillAppear中,你应该在分配startDate之前在那里(或者更好,在viewDidLoad中)而不是在buttonPressed - Method中初始化它。