2010-10-26 106 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()中初始化()