代碼:
-(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];
}
}
但是運行這段代碼之後最後一行報錯:[imagesremoveObjectsAtIndexes:randomIndex];
Implicit Conversion of 'int' to 'NSindexset' is disallowed with ARC
image是NSString的數組,不是UIImage的數組
試試看
UIImage *img = [UIImage imageNamed:[images objectAtIndex:randomIndex]];