2012-05-15 2 views
1

3 개의 UIButton이있는 NSMutableArray와 50 개의 UIButton이있는 두 번째 NSMutableArray가 있습니다. 나는이 세 UIButtons 타이틀을 Character로 Character로 할당하고 싶다. SecondArray의 각 UIButton에 배치하고 싶다.하지만 두 번째 배열의 50 개 UIButton에는 이미 하나의 Character.My가있다. 기본 목적은 우선 각각의 길이를 확인하고 싶다. 내 첫 번째 배열의 uibutton 제목, 인덱스에 의해 색인에 의해 두 번째 배열의 UIButton에 할당합니다. (Exp의 경우 첫 번째 단추의 제목이 "스택"이고 두 번째 "끝"및 세 번째 "흐름"아니오 인덱스의 UIButtons 할당 0-5, 13-16,35-38에서 두 번째 배열). 내 첫 번째 배열 3 UIBttons 있습니다. 코드입니다.무작위 UIButtons NSMutableArray의 제목을 NSMutablearray의 UIButton에 붙여 넣습니다.

NSMutableArray *buttons = [NSMutableArray arrayWithObjects:btn1, btn2, btn3, nil]; 
      [btn1 setTitle:@"stack" forState:UIControlStateNormal]; 
      [btn2 setTitle:@"over" forState:UIControlStateNormal]; 
      [btn3 setTitle:@"flow" forState:UIControlStateNormal]; 

여기 내 코드가 있습니다. 50 개의 UIButtons를 만들 수 있습니다.

-(void)button:(id)sender { 
    saveBtn = [[NSMutableArray alloc] init]; 
    arr=[[NSArray alloc] 
    initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L", 
    @"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil]; 

    for (int i=0; i<50; i++) { 
      btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
      btn.frame = CGRectMake(spacex, spacey, 30.0, 30.0); 
      int idx; 
      idx = arc4random()%[arr count]; 
       NSString* titre = [arr objectAtIndex:idx]; 
      [btn setTitle:titre forState:UIControlStateNormal]; 
      spacex = spacex + 30; 
      [saveBtn addObject:btn]; 
      [self.view addSubview:btn]; 

     } 


      } 

어떤 지침서를 통해 UIButton 타이틀을 특정 색인의 UIButtons에 어떻게 할당 할 수 있습니까? 고맙습니다

+0

이벤트 처리기 메서드'- (void) button : (id) sender'에서 버튼 배열을 만드는 것이 좋지 않다고 생각합니다. 아니면 의도적인가? (일종의 init 또는 loadView 메소드에서) 버튼을 생성하고 이벤트 처리기에서만 제목을 할당 할 수 있습니다. 어쨌든 : 귀하의 주요 질문에 대해 : 당신이 정확히 무엇을 달성하고자하는 좀 더 정교한 수 있습니까? 특정 제목의 버튼 제목을 '[UIButton *] [saveBtn objectAtIndex : index] setTitle : @ "title"forState : UIControlStateNormal];'과 같이 지정할 수 있습니다. 확실하지 않은 경우 귀하의 질문 :) –

+0

정말 질문 해달라고 ... 당신은 "Z"로 무작위로 제목과 50 버튼을 추가하고 saveBtn에 저장하고, 그들을 다른 x 좌표로 모두 표시 ? 문제가 무엇입니까? – Martol1ni

+0

Dear u right하지만이 무작위 타이틀을 "A"에서 "Z"로 바꾸려는 uibutton에서 첫 번째 Array uibutton 제목을 "stack", "over"및 "flow"특정 index.which에 언급하고 싶습니다. 문제의. – jamil

답변

2

고맙습니다. 모든 사람을 도와주는 고맙습니다.하지만 여기있는 코드는 코드입니다. 다른 사람에게 도움이 될 수 있습니다.

- (void)getbuttontitle { 
    for(int i = 0;i<[buttons count];i++) 
    { 
    if(i == 0) 
    { 
    UIButton *b1 = [buttons objectAtIndex:0]; 
     a = (NSString *)[[b1 titleLabel] text]; 
     [storewords addObject:a]; 
     NSLog(@"b=%@",a); 
     } 
    else if(i == 1) 
    { 

    } 
    else if(i == 2) 
    { 

    } 
} 
} 

    for (NSString *str in storewords) { 
     if (str==a) { 
      int j = 0; 
      int randn= (arc4random()%5)+1 ; 
      NSLog(@"n=%d",randn); 
      for (int i=randn; i<[a length]+randn; i++) { 
       NSLog(@"a=%@",a); 
       NSString *ichar = [NSString stringWithFormat:@"%c", [a characterAtIndex:j]]; 
       UIButton* b = [saveBtn objectAtIndex:i]; 
       NSLog(@"a=%@",ichar); 
       j++; 
       [b setTitle:ichar forState:UIControlStateNormal]; 
      } 
     } 

     if (str==c) { 


     } 

     if (str==d) { 

     }  
    } 
관련 문제