Wednesday, 9 January 2013

Calendar - Multiple month

///Here is method to add multiple month into scroll view as like iPad Reminder application
//Replace your scroll view name

-(NSDate *)getCurrentDate{
   
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
   
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setCalendar:calendar];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
   
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
   
    return [NSDate dateWithTimeInterval:0 sinceDate:[dateFormatter dateFromString:[dateFormat stringFromDate:[NSDate date]]]];
}
-(void)addCalander
{
    NSDate *yourDate=[self getCurrentDate];
   
    float s=7000;
    float h=0;
    float heightSC=0;
   
    for(int m=0;m<=11;m++)
    {
        TKCalendarMonthView *calendar1=[[TKCalendarMonthView alloc] init];
        calendar1.delegate = self;
        calendar1.dataSource = self;
       
       
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate];
        NSInteger theDay = [todayComponents day];
        NSInteger theMonth = [todayComponents month];
        NSInteger theYear = [todayComponents year];
       
        // now build a NSDate object for yourDate using these components
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setDay:theDay];
        [components setMonth:theMonth];
        [components setYear:theYear];
        NSDate *thisDate = [gregorian dateFromComponents:components];


        h=calendar1.frame.size.height+2;
       
        // now build a NSDate object for the next day
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        [offsetComponents setMonth:-1];
        NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
        [calendar1 selectDate:nextDate];
        s=s-calendar1.frame.size.height+2;
               
        yourDate=nextDate;
        heightSC=heightSC+calendar1.frame.size.height+2;
    }
   
   
    yourDate=[self getCurrentDate];
    s=heightSC;
   
    for(int m=11;m>=0;m--)
    {
        TKCalendarMonthView *calendar1=[[TKCalendarMonthView alloc] init];
        calendar1.delegate = self;
        calendar1.dataSource = self;
       
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate];
        NSInteger theDay = [todayComponents day];
        NSInteger theMonth = [todayComponents month];
        NSInteger theYear = [todayComponents year];
       
        // now build a NSDate object for yourDate using these components
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setDay:theDay];
        [components setMonth:theMonth];
        [components setYear:theYear];
        NSDate *thisDate = [gregorian dateFromComponents:components];
        h=calendar1.frame.size.height;
        // now build a NSDate object for the next day
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        [offsetComponents setMonth:-1];
        NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
       
        [calendar1 selectDate:nextDate];
        s=s-calendar1.frame.size.height;
        UIView *view_c=[[UIView alloc]initWithFrame:CGRectMake(0, s, 224, calendar1.frame.size.height)];
       
        [self.view addSubview:scroll];
        [view_c addSubview:calendar1];
        [scroll addSubview:view_c];
        yourDate=nextDate;
    }
   
   
    //NSLog(@"s out is %f",s);
    yourDate=[self getCurrentDate];
    s=heightSC;
    for(int m=11;m<35;m++)
    {
        TKCalendarMonthView *calendar1=[[TKCalendarMonthView alloc] init];
        calendar1.delegate = self;
        calendar1.dataSource = self;
       
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate];
        NSInteger theDay = [todayComponents day];
        NSInteger theMonth = [todayComponents month];
        NSInteger theYear = [todayComponents year];
        int fla=1;
        if(theDay==1){
            fla=0;
        }
        // now build a NSDate object for yourDate using these components
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setDay:theDay];
        [components setMonth:theMonth];
        [components setYear:theYear];
        NSDate *thisDate = [gregorian dateFromComponents:components];
       
        // now build a NSDate object for the next day
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        if(fla==0)
        {
            [offsetComponents setDay:32];
        }
        else
        {
            [offsetComponents setMonth:1];
        }
        NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
        if(m==11)
        {
            nextDate=yourDate;
            calendarScrollingPosition = s;
        }
        [calendar1 selectDate:nextDate];
       
        UIView *view_c=[[UIView alloc]initWithFrame:CGRectMake(0, s, 224, calendar1.frame.size.height)];
       
       
        s=s+calendar1.frame.size.height+2;
       
        [self.view addSubview:view_c];
        [scroll addSubview:view_c];
        [view_c addSubview:calendar1];
       
        yourDate=nextDate;
    }
   
    scroll.contentSize=CGSizeMake(150, s);
    [scroll setShowsHorizontalScrollIndicator:NO];
    [scroll setShowsVerticalScrollIndicator:NO];
   
    [scroll setContentOffset:CGPointMake(0, calendarScrollingPosition) animated:NO];
}

No comments:

Post a Comment