2011-10-25 2 views
0

나는이 다음과 같은 코드에 붙어 :목표 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 루프가 그 것이다 :

  • 코드를 반복하지 않으려 위치 제목, 배경을 설정처럼, 대상 등

을 추가하거나 간단한/짧은 코드가 있습니까 ?

+0

어떻게 다른 버튼이 각 실행에있을 수 - 코드에서 다음 몇 가지 조정 - 몇 가지 선택이 당신이 엑스 코드/인터페이스 빌더는 GUI를 구축하는 데 사용할 수없는 경우? – Mark

답변

3

UIButtons를 할당했지만 초기화하지 않았습니다.

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 10, 10)]; 

은 또한 '[myArray의 objectAtIndex : I]의 모든 인스턴스를 대체 루프 다음 ​​

UIButton *currentButton = [myarray objectAtIndex:i]; 

에있는 UIButton 포인터를 캐시 할 필요와'currentButton '

0
btn1 = [UIButton alloc]; 

당신은 ALLOC 그렇지 않으면 당신은 단지 할당 된 쓰레기 메모리

btn1 = [[UIButton alloc] init]; 
또한

[myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build 

는 객체가 어떤 클래스 모르기 때문에 실패하고 따라서 경우 이후에 초기화하기 위해서 필요 setFrame :을 호출 할 수 있습니다. UIView 하위 클래스 인 버튼으로 캐스팅해야합니다.

UIButton *b = (UIButton *)[myarray objectAtIndex:i]; 
b.frame = CGRectMake(i* 110+25, 60, 100, 100); 
0

이 작업을 시도 할 수 있습니다 ?

for (int i =0; i < [myarray count]; i++) 
{ 
    UIButton *b = [[UIButton alloc] initWithFrame: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]; 
} 

버튼을 잊어 버린 것 같습니다. 여러 가지 방법으로 초기화 할 수 있습니다. 초기화, initWithFrame 및 initWithCoder

0
[myarray objectAtIndex:i].frame = CGRectMake(i* 110+25, 60, 100, 100);// error, cant build 

는 캐스트에 액세스하기 위해 도트 구문을 사용할 수 없기 때문에 당신에게 오류를 제공 (또는이 경우, id) 객체이다. 이 줄을 제거하고이 b을 설정 한 후 다음을 넣으면 :

b.frame = CGRectMake(i* 110+25, 60, 100, 100); 

을 당신은 더 이상 오류가 발생하지 않습니다.

for (UIButton *b in myArray) 

다른 답변자 당신이 처음에 제대로 버튼을 생성되지 않는다는 지적에 올 - buttonWithType 또는 allocinitWithFrame를 사용

또한 빠른 열거를 사용하여 루프를 최적화 할 수 있습니다.

0
UIButton *btn1 = [[UIButton alloc] init]; 
    UIButton *btn2 = [[UIButton alloc] init]; 
    UIButton *btn3 = [[UIButton alloc] init]; 
    UIButton *btn4 = [[UIButton alloc] init]; 
    UIButton *btn5 = [[UIButton alloc] init]; 
    UIButton *btn6 = [[UIButton alloc] init]; 
    NSArray *myarray = [NSArray arrayWithObjects:btn1, btn2, btn3, btn4, btn5, btn6,nil]; 

    for (int i =0; i < [myarray count]; i++) 
    { 
     NSLog(@"What: %@", [myarray objectAtIndex:i]);   
     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; 
     float red = 0.33; 
     float green = 0.82; 
     float blue = 0.48; 

     //Color values are float values between 0 and 1; 

     b.titleLabel.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; 
     b.tag = i+1; 
     [b addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown]; 
     [self.view addSubview: b]; 
     [b release]; 
    } 
관련 문제