Domanda

I am trying to do something I thought would be relatively simple and get the current day of the week, then make some checks against that day when buttons are tapped. to get the day of the week I have used the following code in my viewDidLoad method:

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];

The code returns me two NSIntegers which are the current day of the week and the current day of the month, which is what I wanted. I now want to have a series of buttons, which when tapped displays a certain webpage in a UIWebView depending on the day of the week.

I was hoping to do a simple:

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

But I can't seem to get access to the 'weekday' NSInteger which was created in the viewDidLoad method. Im sure I am missing something very simple. Any advice?

È stato utile?

Soluzione

The problem is weekday is out of scope when you try to use it in the btnClicked method. Try creating a property for it in the interface section like this:

@property (nonatomic, retain) NSInteger weekday;

Then @synthesize in the implementation

Or alternatively just add it as an instance variable:

@interface class : NSObject {
    NSInteger weekday;
}
@end
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top