2 u010025993 u010025993 于 2013.05.21 11:28 提问

随机分配4个图片到iPhone按钮中

代码:

-(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

2个回答

Chengzi_963
Chengzi_963   2013.05.21 14:55
已采纳

image是NSString的数组,不是UIImage的数组

试试看

UIImage *img = [UIImage imageNamed:[images objectAtIndex:randomIndex]];
xinyoulingxi1_tong
xinyoulingxi1_tong   2013.05.21 13:38

removeObjectsAtIndexe 需要的是 NSIndexSet,不是整形:

使用[images removeObjectAtIndex:randomIndex];

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

Csdn user default icon
上传中...
上传图片
插入图片