Your code for addition is correct. However relying on string to date conversion is creating problems. I'll advise you to take a NSDate
object as instance variable and then update this instance variable on going to next or previous day. After updating value you can use dateformatter to retrieve string from this date. Right now when you use your label's text to generate date, it creates problems.
So that's how you can do it
// currentDate is my instance variable.
currentDate = [[NSDate date] retain];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"EE. d MMMM YYYY"];
dateLabel.text = [dateFormat stringFromDate:currentDate];
And that's how your nextdayfunctionwill look Like
(IBAction)nextDay:(id)sender {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"EE. d MMMM YYYY"];
currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain];
NSString *dateString = [dateFormat stringFromDate:currentDate];
dateLabel.text = dateString;
}
And here is previous day
enter code here
(IBAction)previousDay:(id)sender {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:@"EE. d MMMM YYYY"];
currentDate = [[theCalendar dateByAddingComponents:dayComponent toDate:currentDate options:0] retain];
NSString *dateString = [dateFormat stringFromDate:currentDate];
dateLabel.text = dateString;
}