随机4图像到4个不同的按钮为iPhone

时间:2013-05-20 17:51:50

标签: iphone ios

这是我的代码:

-(void)RandomButtonImage {

    NSMutableArray *images = [NSMutableArray arrayWithObjects:@"Character_1_1.png",@"Character_1_2.png",@"Character_2_1.png",@"Character_2_2.png" nil];

    NSArray *Buttons = [NSArray arrayWithObjects:AnsButton1,AnsButton2,AnsButton3,AnsButton4, nil];

    for (UIButton *btn in Buttons) {
        int randomIndex = random() % images.count;
        UIImage *img = [images objectAtIndex:randomIndex];
        [btn setImage:img forState:UIControlStateNormal];
        [images removeObjectsAtIndexes:randomIndex];
    }
}

我试着查看其他帖子,但我的编码完全一样。

2 个答案:

答案 0 :(得分:3)

imagesNSString的数组,而不是UIImage的数组。

试试这个:

UIImage *img = [UIImage imageNamed:[images objectAtIndex:randomIndex]];

答案 1 :(得分:0)

removeObjectsAtIndexes想要一个NSIndexSet,而不是一个整数:

使用[images removeObjectAtIndex:randomIndex];

[images removeObjectsAtIndexes:[NSIndexSet indexSetWithIndex:randomIndex]];