Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 通过数组将UIImage分配给UIButtons_Objective C_Ios4_Uibutton_Uiimage_Nsmutablearray - Fatal编程技术网

Objective c 通过数组将UIImage分配给UIButtons

Objective c 通过数组将UIImage分配给UIButtons,objective-c,ios4,uibutton,uiimage,nsmutablearray,Objective C,Ios4,Uibutton,Uiimage,Nsmutablearray,我在数组中有10个图像和10个按钮,我只想将图像随机设置为按钮 我试过了,但没有成功 int index = arc4random() % 10; UIImage *img = [UIImage imageNamed:[arrayAnsewrImages objectAtIndex:index]]; [self.btnAnswerImage1 setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];

我在数组中有10个图像和10个按钮,我只想将图像随机设置为按钮

我试过了,但没有成功

int index = arc4random() % 10;

UIImage *img = [UIImage imageNamed:[arrayAnsewrImages objectAtIndex:index]];

[self.btnAnswerImage1 setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
以下是方法代码,m使用:

[self.arrayQuestionImages addObject:@"bear.jpg"];
[self.arrayQuestionImages addObject:@"girraf.jpg"];
[self.arrayQuestionImages addObject:@"hippo.jpg"];
[self.arrayQuestionImages addObject:@"monkey.jpg"];
[self.arrayQuestionImages addObject:@"piglet.jpg"];
[self.arrayQuestionImages addObject:@"pumba.jpg"];
[self.arrayQuestionImages addObject:@"rabbit.jpg"];
[self.arrayQuestionImages addObject:@"simba.jpg"];
[self.arrayQuestionImages addObject:@"snake.jpg"];
[self.arrayQuestionImages addObject:@"tigger.jpg"];
[self.arrayQuestionImages addObject:@"turtle.jpg"];
int index = arc4random() % 10;

UIImage *img = [UIImage imageNamed:[arrayQuestionImages objectAtIndex:index]];


switch (index)
{   

    case 0:
    {   
        //[self.btnQuestionImage1 setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
        [self.btnAnswerImage1 setImage:img forState:UIControlStateNormal];

    }
        break;
    case 1:
    {
        [self.btnQuestionImage2 setImage:img forState:UIControlStateNormal];    
    }               
        break;
    case 2:
    {
        [self.btnQuestionImage3 setImage:img forState:UIControlStateNormal];    

    }   
        break;
    case 3:
    {
        [self.btnQuestionImage4 setImage:img forState:UIControlStateNormal];

    }
        break;
    case 4:
    {
        [self.btnQuestionImage5 setImage:img forState:UIControlStateNormal];    

    }   
        break;  
    case 5:
    {
        [self.btnQuestionImage6 setImage:img forState:UIControlStateNormal];    

    }   
        break;  
    case 6:
    {
        [self.btnQuestionImage7 setImage:img forState:UIControlStateNormal];    

    }   
        break;  
    case 7:
    {
        [self.btnQuestionImage8 setImage:img forState:UIControlStateNormal];

    }   
        break;  
    case 8:
    {
        [self.btnQuestionImage9 setImage:img forState:UIControlStateNormal];    

    }   
        break;  
    case 9:
    {
        [self.btnQuestionImage10 setImage:img forState:UIControlStateNormal];   

    }   
        break;  



    default:
    {
        [self.btnQuestionImage1 setImage:img forState:UIControlStateNormal];

    }
        break;
} 
}

我的每个按钮上都有空白/无图像。请向我推荐任何选项。

UIImage*img=[UIImage-imageNamed:[ArrayanSwerimagenes-objectAtIndex:index];
[self.btnAnswerImage1 setBackgroundImage:img for状态:UIControlStateNormal]

您需要将图像名称放入数组中


您试图将uiimage对象传递给imagedNamed构造函数

我觉得代码中没有什么奇怪的地方,但有几点我要检查一下

您对[UIImage ImageName:]的调用需要将字符串作为参数,但它至少显示为您传入UIImage对象的数组名称。代码应为以下代码之一:

UIImage *img = [UIImage imageNamed:[arrayAnswerImageNames objectAtIndex:index]];  // Note "Names" array
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.questionImageNames = [NSArray arrayWithObjects:@"bear.jpg", @"girraf.jpg", @"hippo.jpg", @"monkey.jpg", @"piglet.jpg", @"pumba.jpg", @"rabbit.jpg", @"simba.jpg", @"snake.jpg", @"tigger.jpg", @"turtle.jpg", nil];


    NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:10];

    for (int i = 0; i < NUM_BUTTONS; i++) {
        NSUInteger index = arc4random() % NUM_BUTTONS;

        UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
        newButton.frame = CGRectMake(i * x, i * y, width, height);    // Whatever position and size you need...

        UIImage *buttonImage = [UIImage imageNamed:[self.questionImageNames objectAtIndex:index]];
        [newButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

        [self.view addSubview:newButton];
        [buttonArray addObject:newButton];
    }

    self.myButtons = buttonArray;   // Where myButtons is a NSArray property of your class, to store references to the buttons.
}
或:

取决于您希望如何构建“图像”阵列;使用图像或图像名称。另外,我假定按钮的框架已设置,并且它们已添加到视图中

希望你把它整理好

编辑

只是看看你的附加代码。只是想一想-您是否已初始化阵列问题图像?确保您调用类似于以下内容的内容:

self.arrayQuestionImages = [NSMutableArray arrayWithCapacity:10];
在添加对象之前

此外,以上面所示的方式使用switch语句看起来有点模糊。就我个人而言,我会做如下事情:

UIImage *img = [UIImage imageNamed:[arrayAnswerImageNames objectAtIndex:index]];  // Note "Names" array
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.questionImageNames = [NSArray arrayWithObjects:@"bear.jpg", @"girraf.jpg", @"hippo.jpg", @"monkey.jpg", @"piglet.jpg", @"pumba.jpg", @"rabbit.jpg", @"simba.jpg", @"snake.jpg", @"tigger.jpg", @"turtle.jpg", nil];


    NSMutableArray *buttonArray = [NSMutableArray arrayWithCapacity:10];

    for (int i = 0; i < NUM_BUTTONS; i++) {
        NSUInteger index = arc4random() % NUM_BUTTONS;

        UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
        newButton.frame = CGRectMake(i * x, i * y, width, height);    // Whatever position and size you need...

        UIImage *buttonImage = [UIImage imageNamed:[self.questionImageNames objectAtIndex:index]];
        [newButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

        [self.view addSubview:newButton];
        [buttonArray addObject:newButton];
    }

    self.myButtons = buttonArray;   // Where myButtons is a NSArray property of your class, to store references to the buttons.
}

不成功怎么办?您从arc4random获得的功能在看到附加代码后对我的答案进行了新的编辑。甚至[self.btnAnswerImage1 setImage:img forState:UIControlStateNormal];感谢您的努力,但我的愚蠢之处在于:我忘记初始化数组:D