Frage

I have several UIButtons created from an NSMutableArray. I'm trying to customize each UIButton with a unique image.

On the following line I'm not sure of the syntax to use to set an image for each button from the image array respectively:

[btn setImage:[UIImage imageNamed:[myImages objectAtIndex:3]] forState:UIControlStateNormal];

here's the rest of my code:

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

for(int i = 0; i < 8; i++)
{
    // Custom UIButton
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setFrame:CGRectMake(0.0f, 2.0f, 52.0f, 52.0f)];
    [btn setTitle:[NSString stringWithFormat:@"Button %d", i+1] forState:UIControlStateNormal];

    NSArray * myImages = [NSArray arrayWithObjects:@"category0.png", @"category-clothing1.png" , @"category2.png", @"category3.png", nil];

    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:3]] forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [buttonArray addObject:btn];

}

I would also like a different selector to be called for each button.

War es hilfreich?

Lösung

Something like this? I just saw your post about needing different actions for each button but I would instead check the sender of the button and adjust the code accordingly inside your (buttonPressed:) method. You should also make sure you have the appropriate amount of images for your button array so a better method may be to only create the amount of buttons based on your image array count. (answer edited to reflect this). I also wanted to add you should get in the habit of also changing the highlighted state of the image when changing the normal state so when you press the image it doesn't go blank.

NSMutableArray* buttonArray = [NSMutableArray array];
NSArray * myImages = [NSArray arrayWithObjects:@"category0.png", @"category-clothing1.png" , @"category2.png", @"category3.png", nil];

for(int i = 0;i < [myImages count]; i++)
{
    // Custom UIButton
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setFrame:CGRectMake(0.0f, 2.0f, 52.0f, 52.0f)];
    [btn setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [buttonArray addObject:btn];
}

Andere Tipps

This code contains image and selectors from array. (This code is not compiled-check, may contain some small issue, sorry for inconvenience)

NSMutableArray* buttonArray = [NSMutableArray array];
NSArray * myImages = [NSArray arrayWithObjects:@"category0.png", @"category-clothing1.png" , @"category2.png", @"category3.png", nil];


// You need to assign string name and that should be your method name.
NSMutableArray *selectors=[[NSMutableArray alloc]initWithArray:@"sel1",@"sel2",@"sel3",@"sel4", nil];


for(int i=0; i<[myImages count]; i++){
    // Custom UIButton
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0.0f, 2.0f, 52.0f, 52.0f)];
    [btn setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateNormal];

    SEL selector = selectorFromString([selectors objectAtIndex:i]);
    [btn addTarget:self action:@selector(selector) forControlEvents:UIControlEventTouchUpInside];

    [buttonArray addObject:btn];
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top