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 a better approach to me.
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 in the right dire开发者_开发知识库ction? Is there a better way?
Maybe you'll find these links helpful
- Kal
- GCCalendar
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)
精彩评论