gpt4 book ai didi

ios - UIButton 不显示我的数组的 UIbuttons 背景图像

转载 作者:行者123 更新时间:2023-11-28 22:12:51 26 4
gpt4 key购买 nike

The Dice is a button and when you touch it it rolles the dice and show the result on the dice button .因此,它将通过使用数组和 random() 触摸 UIButton 与 IBAction 来显示按钮上随机 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[index] forState:UIControlStateNormal];  

由于您的 _diceArray 包含 UIImage 而不是图像名称

关于ios - UIButton 不显示我的数组的 UIbuttons 背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22474025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com