6

Here is my piece of code that creates buttons programmatically:

NSArray *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:@"Cover_0.png"],
                      [UIImage imageNamed:@"Cover_1.png"],
                      [UIImage imageNamed:@"Cover_2.png"],
                      [UIImage imageNamed:@"Cover_3.png"],
                      [UIImage imageNamed:@"Cover_4.png"],
                      [UIImage imageNamed:@"Cover_5.png"],
                      [UIImage imageNamed:@"Cover_6.png"],
                      [UIImage imageNamed:@"Cover_7.png"],nil];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];

[button setImage:buttonImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;

But while running it I can't get in simulator and its throwing a Signal Sigabart error. Can anyone help?

GingerHead
  • 8,130
  • 15
  • 59
  • 93
Vishnu
  • 2,243
  • 2
  • 21
  • 44

5 Answers5

7

To create buttons programmatically using array of selectors you can use this snippet of code:

//define Strategy

@interface DataItemStrategy : NSObject
@property(nonatomic, assign) SEL someSelector;
@end

@implementation DataItemStrategy

@synthesize someSelector = _someSelector;

-(id)initWithSelector:(SEL)someSelector
{
    self = [super init];
    if (self)
    {
        self.someSelector = someSelector;
    }
    return self;
}

@end


-(void)createButtons
{
    NSArray *buttonImages =[NSArray arrayWithObjects:[UIImage imageNamed:@"Cover_0.png"],
                          [UIImage imageNamed:@"Cover_1.png"],
                          [UIImage imageNamed:@"Cover_2.png"],nil];

    NSArray *dataStrategies = [NSArray arrayWithObjects:
                                   [[[DataItemStrategy alloc] initWithSelector:@selector(buttonAction0:)] autorelease],
                                   [[[DataItemStrategy alloc] initWithSelector:@selector(buttonAction1:)] autorelease],
                                   [[[DataItemStrategy alloc] initWithSelector:@selector(buttonAction2:)] autorelease], nil];

    CGRect buttonFrame = CGRectMake(0, 0, 50, 50);
    for (NSInteger i = 0; i < [buttonImages count]; i++)
    {
        buttonFrame.origin.y += 50;
        UIImage *buttonImage = [buttonImages objectAtIndex:i];
        DataItemStrategy *dataStrategie = [dataStrategies objectAtIndex:i];
        NSString *title = [NSString stringWithFormat:@"%d", i];
        UIButton *button = [self buttonWithFrame:buttonFrame image:buttonImage action:dataStrategie.someSelector title:title];
        [self.view addSubview:button];
    }    
}

-(UIButton *)buttonWithFrame:(CGRect)buttonFrame image:(UIImage *)buttonImage action:(SEL)buttonAction title:(NSString *)title
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = buttonFrame;
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.titleLabel.font = [button.titleLabel.font fontWithSize:50];

    [button setImage:buttonImage forState:UIControlStateNormal];
    [button addTarget:self action:buttonAction forControlEvents:UIControlEventTouchUpInside];
    return button;
}
Igor
  • 4,235
  • 3
  • 34
  • 32
2

You can't set an array of images to the UIButton image property. If you look at the documentation of UIButton you see that it expects a UIImage.

Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
2

What is the index in your code?? Set This:

[button setBackgroundImage:[buttonImage objectAtIndex:index] forState:UIControlStateNormal];
Mansi Panchal
  • 2,357
  • 18
  • 27
1

Modify the code as below to set first image to button/ you can use for loop to create and set images to uibuttons

NSArray *buttonImage=[NSArray arrayWithObjects:[UIImage imageNamed:@"Cover_0.png"],
                  [UIImage imageNamed:@"Cover_1.png"],
                  [UIImage imageNamed:@"Cover_2.png"],
                  [UIImage imageNamed:@"Cover_3.png"],
                  [UIImage imageNamed:@"Cover_4.png"],
                  [UIImage imageNamed:@"Cover_5.png"],
                  [UIImage imageNamed:@"Cover_6.png"],
                  [UIImage imageNamed:@"Cover_7.png"],nil];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setTitle:[NSString stringWithFormat:@"%i", index] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];

[button setImage:(UIImage*)[buttonImage objectAtIndex:1] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;

change line

[button setImage:(UIImage*)[buttonImage objectAtIndex:1] forState:UIControlStateNormal];

This will set image to button. You have sent an array to set image so application will crash

Sumanth
  • 4,913
  • 1
  • 24
  • 39
  • :tks...but if i set as ObjectAtIndex as 1 only Cover_1.png is set to all images but i need each but has individual background images...Can u Help?... – Vishnu Jun 16 '12 at 06:40
  • You need to use [buttonImage objectAtIndex:0] to set image to button For second Button use [buttonImage objectAtIndex:1] Third [buttonImage objectAtIndex:2]; Fourth [buttonImage objectAtIndex:3]; Similary you have to set – Sumanth Jun 16 '12 at 06:56
  • :Tks man...I've got my output by defining objectAtIndex as index...But i tried to fire diffrent action for each button by selecting it...Here is my code for first button – Vishnu Jun 16 '12 at 07:08
  • :can u help me with how to fire an action for each button?..Is it possible by making action by grouping it to array?... – Vishnu Jun 16 '12 at 07:25
1

Try : [button setImage:[buttonImage objectAtIndex:index] forState:UIControlStateNormal];

webmastx
  • 683
  • 1
  • 8
  • 30