2010-10-26 68 views
1

我想使用另一种方法捕获的NSDate变量。经过多次阅读和搜索,我认为我在这个网站上找到了答案:http://www.everydayone.com/2009/08/nsdate-as-global-variable/。我首先尝试在文章中指定的AppDelegate.h中声明变量。在ViewController实现文件中导致2次失败:firstDate和startDate未声明。然后我试着在ViewController.h中声明它们,它允许代码无误地编译。但是,当方法运行如下所示时,应用程序崩溃,并收到以下消息“GDB:Program received signal:EXC_BAD_ACCESS”。如果我取消注释“NSDate *今天”行并使用该变量而不是secondDate,则代码正常工作。你的帮助将不胜感激。全球NSDate变量

.H:

@interface DatesViewController : UIViewController { 
    NSDate  *firstDate; 
    NSDate  *secondDate; 
} 

@property (nonatomic, retain) NSDate *firstDate; 
@property (nonatomic, retain) NSDate *secondDate; 

.M:

@synthesize firstDate; 
@synthesize secondDate; 

-(IBAction)getFirstDate:(id)sender{ 
    firstDate = [picker date]; 
} 


-(IBAction)getSecondDate:(id)sender{ 
    secondDate = [picker date]; 
    //NSDate *today = [[NSDate alloc] initWithTimeIntervalSinceNow:0]; 
    NSTimeInterval interval = [firstDate timeIntervalSinceDate: secondDate]; 

回答

0

尝试参照firstDate和secondDate作为

self.firstDate; 
self.secondDate; 

此外,确保有没有办法让getSecondDate()来在getFirstDate()之前调用;您不检查firstDate是否存在或在getSecondDate()中初始化()