将值存储在nsarray的最后一个索引对象中

时间:2014-06-05 06:28:18

标签: ios objective-c ios7

我在UIView中动态创建了一堆按钮。我在NSMutable数组中添加了所有按钮框架和标签。我有一个像"逃避"我从中提取每个字符并在标签的帮助下随机存储在UIButton中。但问题是它只显示最后一个字母并仅存储在一个按钮中。 请帮帮我。

这是我的代码

   -(void)createButtonFrame
{
    for (float colCount=1;colCount<222.6;) {
        for (float rowCount=1;rowCount<=450;) {
            alphabetButton=[[UIButton alloc]initWithFrame:CGRectMake(rowCount-1,colCount-1,50,27.4)];

            alphabetButton.layer.borderColor=[UIColor grayColor].CGColor;
                alphabetButton.layer.borderWidth = 0.8f;
            alphabetButton.tag=tagButtonValue;

            [masterView addSubview:alphabetButton];
            [btnArray addObject:alphabetButton];
             rowCount+=50;
            tagButtonValue++;
        }
        colCount+=27.4;
    }

}

-(void)puAlphabetFunction
{


    for (int wordCount=0;wordCount<[alphabetArray count]; wordCount++) {
        word=[alphabetArray objectAtIndex:wordCount];
        [self putrandomAlphabet:word];

    }
}

-(void)putrandomAlphabet:(NSString*)wordString
{


   for (int characterCount=0;characterCount<wordString.length;characterCount++) {
       retreiveCharacter = [wordString substringWithRange:NSMakeRange(characterCount, 1)];
       srandom( time( NULL ) );
       int  randomNumber = (random() % [btnArray count]);
 btnvalue=(UIButton *)[self.view viewWithTag:randomNumber+1];
       if (firstTime==0) {
           lastIndex=randomNumber;
           [btnvalue setTitle:retreiveCharacter forState:UIControlStateNormal];
           firstTime++;

       }
       else
       {

             [btnvalue setTitle:retreiveCharacter forState:UIControlStateNormal];
       }
       [btnvalue setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btnvalue.titleLabel setFont:[UIFont fontWithName:@"Helvetica Neue"     size:17.0]];


    }
}

1 个答案:

答案 0 :(得分:0)

你不应该使用random()函数。请改用arc4random()。它将以标准化方式生成随机数。