Created
May 26, 2011 05:16
-
-
Save wezm/992594 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This block is invoked when a row in the table view is chosen. | |
// The table view lists the weekdays from Mon-Sun, so the desiredWeekday line | |
// is about converting to a system where Sunday is 1 | |
controller.didSelectObjectBlock = ^(id obj, NSUInteger idx) { | |
NSInteger desiredWeekday = (idx + 2) % 7; | |
// Set self.date to a date corresponding to the nearest chosen | |
// weekday in the future | |
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDate *today = [NSDate date]; | |
NSDateComponents *todayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:today]; | |
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; | |
[offsetComponents setWeekday:0 - ([todayComponents weekday] - desiredWeekday)]; | |
if ([offsetComponents weekday] < 0) { | |
// If the offset would make the date in the past, add 7 to get into | |
// the following week | |
[offsetComponents setWeekday:[offsetComponents weekday] + 7]; | |
} | |
this.date = [calendar dateByAddingComponents:offsetComponents toDate:today options:0]; | |
[offsetComponents release]; | |
[calendar release]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment