I'm totally new to iOS development so forgive me if this question is stupid, but its wrecking my head.
I've set myself the task of building a Hangman Game for iPhone. When the game starts I want the letters of the alphabet to randomly populate into 26 UIImageView
's that I have already setup in my ViewController
.
I have 2 NSMutableArray
's setup. One holds the UIImage reference for the Alphabet images and the other is an array of strings containing the names of all the UIImageViews
.
What I'm looking to do is run through a for
or a while
loop and have the images assigned to the UIImageViews
.
Here is the code I'm currently using.
- (void)randomizeAlphabet
{
// Code to randomise Letters on start up of the App.
NSMutableArray *alphabet = [[NSMutableArray alloc] initWithObjects:
[UIImage imageNamed:@"A.png"],
[UIImage imageNamed:@"B.png"],
[UIImage imageNamed:@"C.png"],
[UIImage imageNamed:@"D.png"],
[UIImage imageNamed:@"E.png"],
[UIImage imageNamed:@"F.png"],
[UIImage imageNamed:@"G.png"],
[UIImage imageNamed:@"H.png"],
[UIImage imageNamed:@"I.png"],
[UIImage imageNamed:@"J.png"],
[UIImage imageNamed:@"K.png"],
[UIImage imageNamed:@"L.png"],
[UIImage imageNamed:@"M.png"],
[UIImage imageNamed:@"N.png"],
[UIImage imageNamed:@"O.png"],
[UIImage imageNamed:@"P.png"],
[UIImage imageNamed:@"Q.png"],
[UIImage imageNamed:@"R.png"],
[UIImage imageNamed:@"S.png"],
[UIImage imageNamed:@"T.png"],
[UIImage imageNamed:@"U.png"],
[UIImage imageNamed:@"V.png"],
[UIImage imageNamed:@"W.png"],
[UIImage imageNamed:@"X.png"],
[UIImage imageNamed:@"Y.png"],
[UIImage imageNamed:@"Z.png"],
nil];
NSMutableArray *imgViewsAlphabet = [[NSMutableArray alloc] initWithObjects:
@"letterA",
@"letterB",
@"letterC",
@"letterD",
@"letterE",
@"letterF",
@"letterG",
@"letterH",
@"letterI",
@"letterJ",
@"letterK",
@"letterL",
@"letterM",
@"letterN",
@"letterO",
@"letterP",
@"letterQ",
@"letterR",
@"letterS",
@"letterT",
@"letterU",
@"letterV",
@"letterW",
@"letterX",
@"letterY",
@"letterZ"
, nil];
//Randomly assign the letter from the array
while ([alphabet count] > 0 )
{
int index = arc4random_uniform([alphabet count]);
[(UIImageView *)[imgViewsAlphabet objectAtIndex:index] setImage:[alphabet objectAtIndex:index]];
[alphabet removeObjectAtIndex:index];
[imgViewsAlphabet removeObjectAtIndex:index];
}
}
The code above seems fine in Xcode but when it compiles it crashes out.
Any help at all would be greatly appreciated.
Cheers, TQ