【发布时间】:2014-03-18 08:34:51
【问题描述】:
。因此,它会通过使用数组和 random() 从图像数组中选择要在图像背景上显示的 IBAction 触摸 UIButton,在按钮上显示随机 1-6 图像的图像
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)diceButton:(UIButton *)sender {
_diceArray=[NSMutableArray arrayWithObjects:
[UIImage imageNamed:@"Dice_1.png"],
[UIImage imageNamed:@"Dice_2.png"],
[UIImage imageNamed:@"Dice_3.png"],
[UIImage imageNamed:@"Dice_4.png"],
[UIImage imageNamed:@"Dice_5.png"],
[UIImage imageNamed:@"Dice_6.png"],nil];
int index = random() % 5 ;
for (int i = 0; i < [_diceArray count]; i++) {
[_diceButton setBackgroundImage:[UIImage imageNamed:[_diceArray objectAtIndex:index]] forState:UIControlStateNormal];
}
}
@end
【问题讨论】:
-
你想做什么?
-
您已经将图像存储在数组中,而不是数组中的名称。所以我喜欢这个 [_diceButton setBackgroundImage:[_diceArray objectAtIndex:index] forState:UIControlStateNormal];
标签: ios objective-c uiviewcontroller uibutton nsarray