HurkNburkS
HurkNburkS

Reputation: 5510

how to add UIImage from a NSMutableArray of images to UIButton using setImage

I have an array of UIImages which I have created like this

UIImage *tButtonImage = [UIImage imageNamed:@"button-measurement.png"];
UIImage *tPButtonImage = [UIImage imageNamed:@"button-parts.png"];
UIImage *oPButtonImage = [UIImage imageNamed:@"button-onboard.png"];
UIImage *dButtonImage = [UIImage imageNamed:@"button-opening.png"];

// then depending on the corresponding values of a NSDicitonary / keyvalue pairs will decide which UIImages will be put into my array like so

NSMutableArray *arrayOfButtonImages = [[NSMutableArray alloc] init];

if ([hasInfoSetIDString isEqualToString:@"T"]) {
    [arrayOfButtonImages addObject:tButtonImage];
}
if ([hasTSetIDString isEqualToString:@"T"]) {
    [arrayOfButtonImages addObject:tPButtonImage];
}
if ([hasOSetIDString isEqualToString:@"T"]) {
    [arrayOfButtonImages addObject:oPButtonImage];
}
if ([hasDSetIDString isEqualToString:@"T"]) {
    [arrayOfButtonImages addObject:dButtonImage];
}

by this point depending on the values in my NSDictionary I might have an array that looks something like this.

NSLog(@"%@", arrayOfButtonImages);

(
    "<UIImage: 0x2001f4c0>",
    "<UIImage: 0x1ed51c20>"
)

I then try to apply those images to the buttons I have, so the buttons are "dynamic" per say.

if (arrayOfButtonImages != nil) {

            for (int i = 0; i < [arrayOfButtonImages count]; i++) {
                if (i == 0) {
                    [jumpButton1P setImage:[arrayOfButtonImages objectAtIndex:i] forState:UIControlStateNormal];
                }
                else if (i == 1) {
                    [jumpButton2P setImage:[arrayOfButtonImages objectAtIndex:i] forState:UIControlStateNormal];
                }
                else if (i == 2) {
                    [jumpButton3P setImage:[arrayOfButtonImages objectAtIndex:i] forState:UIControlStateNormal];
                }
                else if (i == 3) {
                    [jumpButton4P setImage:[arrayOfButtonImages objectAtIndex:i] forState:UIControlStateNormal];
                }
                else if (i == 4) {
                    [jumpButton5P setImage:[arrayOfButtonImages objectAtIndex:i] forState:UIControlStateNormal];
                }
            }
}

so after all that is said and done, the image is not appearing on my UIButton.. I have checked to make sure that the UIButton variable is connected with the UIBUtton object in Interface Builder.. but still something is going wrong.

I have even just tried a simple

UIImage *btnImage1 = [UIImage imageNamed:@"diagram-P.png"];
[jumpButton1P setImage:btnImage1 forState:UIControlStateNormal];

and this dosnt even work.

hopefully this all makes sense.. any help would be greatly appreciated.

Upvotes: 2

Views: 985

Answers (3)

Cameron
Cameron

Reputation: 383

Not to sound redundant, but have you confirmed that the image objects are indeed linked to your project? Press on your project in the Project Navigator, select your app target, press on the Build Phases tab, and expand the "Copy Bundle Resources (x items)" section.

Also, make sure that all of your images are listed there and the filepath next to the items are correct (pointing to your project, not your desktop/pictures folder or something).

Upvotes: 0

Marioea
Marioea

Reputation: 201

Test it again after setting the button type = "Custom" in Interface Builder or manually:

  UIButton *btnOne = [UIButton buttonWithType:UIButtonTypeCustom];

And then call setImage or setBackgroundImage

Hope it helps

Upvotes: 0

Tommy Devoy
Tommy Devoy

Reputation: 13549

Try setting the backgroundImage instead:

[jumpButton1P setBackgroundImage:[UIImage imageNamed:@"diagram-P"] forState:UIControlStateNormal];

Upvotes: 2

Related Questions