私の問題に対するより正確で論理的な答えを見つけました。
アルファが述べたように、配列をシャッフルしてソートされたものを形成する必要があります。たとえば、次のように、インデックスに基づいてランダムな単語を jumbledWord に割り当てる必要があります。
int randomWordIndex = (int)arc4random%[greWordsArray count];
self.jumbledWord = [greWordsArray objectAtIndex:randomWordIndex];
これで、単語の長さに基づいてボタンを作成する準備が整いました。
-(void)buttonsCreation:(int)noOfButtons
{
NSMutableArray *charactersArray = [NSMutableArray array];
self.wordButtons = [[NSMutableArray alloc]initWithCapacity:20];
BOOL record = NO;
int randomNumber;
for (int i=0; [charactersArray count] < jumbledWord.length; i++) //Loop for generate different random values
{
randomNumber = arc4random() % jumbledWord.length;//generating random number
if(i==0)
{
[charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
}
else
{
for (int j=0; j<= [charactersArray count]-1; j++)
{
if (randomNumber ==[[charactersArray objectAtIndex:j] intValue])
record = YES;
}
if (record == YES)
record = NO;
else
[charactersArray addObject:[NSNumber numberWithInt:randomNumber]];
}
}
int arrayValue;
float x = 100.0;
float y = 100.0;
for(int i=0;i<[jumbledWord length];i++)
{
if(i>=0 && i<10)
{
arrayValue = [[charactersArray objectAtIndex:i] intValue];
x = x + 33;
UIButton *characterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
characterButton.frame = CGRectMake(x,175.0, 35.0, 35.0);
[wordButtons addObject:characterButton];
NSString *characterValue = [NSString stringWithFormat:@"%c",[jumbledWord characterAtIndex:arrayValue]];
[characterButton setTitle:characterValue forState:UIControlStateNormal];
[characterButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[characterButton setTag:arrayValue];
[self.view addSubview:characterButton];
}
else if(i>=10 && i<15)
{
y = y + 33;
UIButton *characterButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
characterButton.frame = CGRectMake(y,200.0, 35.0, 35.0);
[wordButtons addObject:characterButton];
NSString *characterValue = [NSString stringWithFormat:@"%c",[jumbledWord characterAtIndex:i]];
[characterButton setTitle:characterValue forState:UIControlStateNormal];
[characterButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[characterButton setTag:i];
[self.view addSubview:characterButton];
}
}
}
x と y の値は、要件によって異なる場合があることに注意してください。単語の長さに基づいて x と y の座標位置を簡単に設定しました。必要に応じて変更できます。ありがとう :)
それが誰かの役に立てば幸いです、幸せなコーディング!!