开发者

Assign UIImages to UIButtons via Array

开发者 https://www.devze.com 2023-04-04 07:45 出处:网络
I have 10 images in array and 10 buttons, all i want is to set the images to buttons randomly. i tried this but not succeeded

I have 10 images in array and 10 buttons, all i want is to set the images to buttons randomly.

i tried this but not succeeded

int index = arc4random() % 10;

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

[self.btnAnswerImage1 setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];

here is the code of method , m using :

[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.开发者_如何学JAVAbtnQuestionImage10 setImage:img forState:UIControlStateNormal];   

    }   
        break;  



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

    }
        break;
} 
}

M Getting Blank/nil image on my every button. Please suggest me any option.


Nothing strikes me as being that odd in your code, but there are a couple of little things I'd check...

Your call to [UIImage imageNamed:] needs to take a string as a parameter, yet it appears (at least by the name of your array) that you are passing in a UIImage object. The code should be one of the following:

UIImage *img = [UIImage imageNamed:[arrayAnswerImageNames objectAtIndex:index]];  // Note "Names" array

or:

UIImage *img = (UIImage *)[arrayAnswerImages objectAtIndex:index];

depending on how you want to build your 'images' array; with images, or imageNames. Also, I presume the frame of your buttons are set, and that they are added to a view?

Hope you get it sorted.


EDIT

Just looked at your additional code. Just a thought - have you initialised your array arrayQuestionImages? Make sure you call something similar to:

self.arrayQuestionImages = [NSMutableArray arrayWithCapacity:10];

prior to adding the objects.

Also, using a switch statement in the way you have shown above looks a bit obfuscated. Personally, I would do something like the following:

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


You need to put the image names in the array.

you are trying to pass a uiimage object to the imagedNamed constructor


UIImage *img = [UIImage imageNamed:[arrayAnswerImageNames objectAtIndex:index]]; [self.btnAnswerImage1 setBackgroundImage:img forState:UIControlStateNormal];

0

精彩评论

暂无评论...
验证码 换一张
取 消