views:

52

answers:

2

Hi,

I have to use a calendar in my iphone application. I tried hard but could not find any inbuilt API in iphone sdk for this purpose. Now i am going to create calendar by myself and need some guidance for it.

I have created 5 rows of 7 buttons(i.e, 35 buttons in all) and will be using it as the date and then later i will add other graphics for the days and month. I have taken these buttons in an array and will be adding/changing their properties in a loop.

I am having a doubt if this is correct approach or if some one could suggest me a better approach.

btnCal29=[[UIButton alloc]initWithFrame:CGRectMake(22.0f, 312.0f, 35.0f, 35.0f)];
btnCal30=[[UIButton alloc]initWithFrame:CGRectMake(62.0f, 312.0f, 35.0f, 35.0f)];
btnCal31=[[UIButton alloc]initWithFrame:CGRectMake(102.0f, 192.0f, 35.0f, 35.0f)];
btnCal32=[[UIButton alloc]initWithFrame:CGRectMake(142.0f, 192.0f, 35.0f, 35.0f)];
btnCal33=[[UIButton alloc]initWithFrame:CGRectMake(182.0f, 192.0f, 35.0f, 35.0f)];
btnCal34=[[UIButton alloc]initWithFrame:CGRectMake(222.0f, 192.0f, 35.0f, 35.0f)];
btnCal35=[[UIButton alloc]initWithFrame:CGRectMake(262.0f, 192.0f, 35.0f, 35.0f)];

arrCalendarbutton = [[NSMutableArray alloc] initWithObjects:btnCal1,btnCal2,btnCal3,btnCal4,btnCal5,btnCal6,btnCal7,btnCal8,btnCal9,btnCal10,btnCal11,btnCal12,btnCal13,btnCal14,nil];

for(int i = 0; i<[arrCalendarbutton count];i++)
{
    [(UIButton *)[arrCalendarbutton objectAtIndex:i] setBackgroundColor:[UIColor lightGrayColor]];
    [(UIButton *)[arrCalendarbutton objectAtIndex:i] addTarget:self action:@selector(cal) forControlEvents:UIControlEventTouchUpInside];
    [(UIButton *)[arrCalendarbutton objectAtIndex:i] setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
    //[(UIButton *)[arrCalendarbutton objectAtIndex:i] setTitle:@"11" forState:UIControlStateNormal];
    [self.view addSubview: (UIButton *)[arrCalendarbutton objectAtIndex:i]];
}

I am using above code to show the buttons(not added code for all buttons here). I will of course be changing the title of the buttons on later changes but for now i am only focusing on design.

Am i going into right direction? or there is a better way?

Thanks

+4  A: 

Maybe you'll find these links helpful

I wouldn't connect every button to a named variable.

do soemthing like this:

NSMuteableArray *array = [[NSMUteableArray alloc] init];

for(int i=0; i<31; i++){
    UIButton *b = ....;
    //customize b
    [array addObject:b];
    [b release];
}

If you are iterating over an array, for-each is recommended instead of

for(int i = 0; i<[arrCalendarbutton count];i++)

do

for(UIButton *b in arrCalendarbutton)
vikingosegundo
+1  A: 

There is also this answer to a similar question.

coob
i found my calendar as kal calender here, thanks
pankaj