1

I have an NSMutableArray with 3 UIButtons and Second NSMutableArray with 50 UIButtons. I want to take these 3 UIButtons Titles to Assign it Character by Character to each UIButton of SecondArray.but Each of these 50 UIButtons of Second Array already have single Character.My Basics purpose is that first of all i want to check the length of each uibutton title of my first Array,then assign it to index by index to UIButton in Second Array.(For Exp if my first Button have titles "stack",second "over" and Third "flow" no i want to Assign UIButtons of index 0-5 ,13-16,35-38 in Second Array) .My first Array that have 3 UIBttons .here is code.

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

Here is my code.To create 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];

        }


            }

Any one guide me how can i assign my UIButton Titles to these UIButtons on Specific Index. Thanx

jamil
  • 2,419
  • 3
  • 37
  • 64
  • I don't think it's a good idea to create array of buttons in event handler method `-(void)button:(id)sender`. Or is this on purpose? You could create buttons before (in some kind of init or loadView method) and only assign their titles in event handler. Anyway: as for your main question: can you elaborate some more what exactly you want to achieve? You can assign the title of button with specific index with `[(UIButton *)[saveBtn objectAtIndex: index] setTitle:@"title" forState: UIControlStateNormal];`. Not sure if that is your question :) – Rok Jarc May 16 '12 at 08:50
  • I dont really get your question... You are adding 50 buttons with random titles from "A" to "Z" and storing them in saveBtn, and then displaying them all with different x coordinates? What is the problem? – Martol1ni May 16 '12 at 16:22
  • Dear u right but i want to replace these random Titles "A" to "Z" from those uibutton where i want to assign my first Array uibutton titles "stack","over" and "flow" on specific index.which i mention in question. – jamil May 16 '12 at 16:32

1 Answers1

2

Thanx for helping Every one ..but i done it here is code.my be it help some one others.

 - (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) {

        }       
    }
jamil
  • 2,419
  • 3
  • 37
  • 64