2012-01-25 49 views
2

我正在嘗試做一些我認爲會比較簡單的事情,並獲取當週的當天,然後在點擊按鈕時針對當天進行一些檢查。得到一週的一天,我都用下面的代碼在我viewDidLoad方法:在我的代碼的其餘部分訪問輸出的'NSInteger'

NSDate *today = [NSDate date]; 
NSCalendar *gregorian = [[NSCalendar alloc] 
         initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *weekdayComponents = 
[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today]; 
NSInteger day = [weekdayComponents day]; 
NSInteger weekday = [weekdayComponents weekday]; 

代碼返回我兩個NSIntegers這是一週中的當前日和月的當前天,這是什麼我想了。我現在想要有一系列按鈕,當按下按鈕時會顯示UIWebView中的特定網頁,具體取決於一週中的哪一天。

我希望做一個簡單:

-(IBAction) btnClicked 
{ 
    if (weekday == 1) 
    { 
     //DO SOMETHING HERE 
    { 
} 

但我似乎無法得到進入「工作日」 NSInteger的這是在viewDidLoad方法創建的。我確定我錯過了一些非常簡單的事情。有什麼建議?

回答

4

問題是,當您嘗試在btnClicked方法中使用它時,工作日超出了範圍。嘗試在這樣的接口部分爲它創造一個屬性:

@property (nonatomic, retain) NSInteger weekday; 

然後@synthesize在實施

或可替代只需將其添加爲一個實例變量:

@interface class : NSObject { 
    NSInteger weekday; 
} 
@end 
+0

+1別不要忘記提及'NSInteger weekday = [weekdayComponents weekday];'應該更改爲'weekday = [weekdayComponents weekday];' – dasblinkenlight

+0

hhmm,這樣做可以讓我訪問它,但它會產生大量關於比較的警告指針和整數之間。它也將NSInteger值設置爲'0',這是不好的。 –

+0

對不起,忘了NSInteger只是一個整數而不是對象。我更新了我的代碼以匹配。 – danielbeard

相關問題