使用NSMutableArray将图像设置为多个UIImageViews

时间:2014-06-09 00:10:07

标签: ios objective-c loops uiimageview

我对iOS开发完全陌生,所以请原谅我,如果这个问题很愚蠢,但它会破坏我的头脑。

我为自己设定了为iPhone制作刽子手游戏的任务。当游戏开始时,我希望字母表中的字母随机填充到我UIImageView中已设置的26 ViewController个字母中。

我有2个NSMutableArray的设置。一个保存字母图像的UIImage参考,另一个是包含所有UIImageViews名称的字符串数组。

我要做的是通过forwhile循环,并将图像分配给UIImageViews

以下是我目前正在使用的代码。

- (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];
}
}

上面的代码在Xcode中似乎很好,但是当它编译时它会崩溃。

任何帮助都将非常感谢。

干杯, TQ

2 个答案:

答案 0 :(得分:1)

您有2个阵列,imgViewsAlphabetalphabet

imgViewsAlphabet是一个字符串数组。

alphabet是UIImage对象的数组。

这一行:

[(UIImageView *)[imgViewsAlphabet objectAtIndex:index] 
  setImage:[alphabet objectAtIndex:index]];

尝试从imgViewsAlphabet数组中获取对象,并向其发送setImage:message。

imgViewsAlphabet数组中有哪些对象?这些对象是否理解setImage方法?

答案 1 :(得分:0)

@DuncanC是对的。看起来您正在寻找具有以随机,非重复方式选择的图像的图像视图阵列。一个很好的简单方法是按顺序构建该数组,然后将其洗牌:

NSMutableArray *imageViewArray = [NSMutableArray array];
for (char aChar = 'A'; aChar <= 'z'; ++aChar)  {
    NSString *imageName = [NSString stringWithFormat:@"%c.png", aChar];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageViewArray addObject:imageView];
}

现在洗牌。有很多方法可以做到这一点,比如Fisher-Yates algorithm。这是a high voted SO answer做到的。简而言之:

NSUInteger count = [imageViewArray count];
for (NSUInteger i = 0; i < count; ++i) {
    NSInteger remainingCount = count - i;
    NSInteger exchangeIndex = i + arc4random_uniform(remainingCount);
    [imageViewArray exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}

这将为您留下一个UIImageViews数组,其图像以随机顺序命名为@&#34; A.png&#34;,@&#34; B.png&#34;等。您需要分配这些框架并将其作为子视图添加到某个视图中才能看到。