나는이 다음과 같은 코드에 붙어 :목표 C : 배열에서 uibutton을 생성하는 중 오류가 발생 했습니까?
switch (i)
case 0: {
/*
.....
repeat this code
.....
}
는 따라서 사용 스위치 케이스 경우, 코드가 매우 긴 :
btn1 = [UIButton alloc];
btn2 = [UIButton alloc];
btn3 = [UIButton alloc];
btn4 = [UIButton alloc];
btn5 = [UIButton alloc];
btn6 = [UIButton alloc];
NSArray *myarray = [NSArray arrayWithObjects:btn1, btn2, btn3, btn4, btn5, btn6,nil];
for (int i =0; i < [myarray count]; i++)
{
NSLog(@"What: %@", [myarray objectAtIndex:i]); // Result UIButton
[myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build
UIButton *b = (UIButton *)[myarray objectAtIndex:i];
b.frame = CGRectMake(i* 110+25, 60, 100, 100);
[b setTitle:@"my button" forState:UIControlStateNormal];
b.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
b.titleLabel.textAlignment = UITextAlignmentCenter;
b.titleLabel.textColor = [UIColor colorWithRed:0x93/255.0 green:0x73/255.0 blue:0x47/255.0 alpha:1.0];
b.tag = i+1;
[b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
[self addSubview: b];
[b release];
}
는 원래 내가 좋아하는, 스위치 케이스를 사용합니다. 이유 I 루프가 그 것이다 :
- 코드를 반복하지 않으려 위치 제목, 배경을 설정처럼, 대상 등
을 추가하거나 간단한/짧은 코드가 있습니까 ?
어떻게 다른 버튼이 각 실행에있을 수 - 코드에서 다음 몇 가지 조정 - 몇 가지 선택이 당신이 엑스 코드/인터페이스 빌더는 GUI를 구축하는 데 사용할 수없는 경우? – Mark