2012-06-16 9 views
7

Ecco il mio pezzo di codice che crea i pulsanti di programmazione:Come impostare un'immagine di sfondo individuale per gruppo di UIButtons livello di programmazione

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; 

Ma durante l'esecuzione di esso non posso ottenere nel simulatore e la sua lanciando un segnale Sigabart errore. Qualcuno può aiutare?

risposta

7

Per creare pulsanti di programmazione che utilizza la matrice di selettori è possibile utilizzare questo frammento di codice:

// definire strategia

@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; 
} 
1

Modificare il codice come indicato di seguito per impostare prima immagine a pulsante/è possibile utilizzare per il ciclo per creare e impostare immagini per 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; 

cambiare linea

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

Questo set di immagini di pulsante . È stato inviato un array per impostare l'immagine in modo che l'applicazione si interrompa

+0

: TKS ... ma se ho impostato come ObjectAtIndex come solo 1 Cover_1.png è impostato su tutte le immagini ma ho bisogno di ognuna, ma ha immagini di sfondo individuali ... Puoi Aiuto? ... – Dany

+0

Devi usare [buttonImage objectAtIndex: 0] per impostare l'immagine sul pulsante Per il secondo Pulsante usa [buttonImage objectAtIndex: 1] Terzo [buttonImage objectAtIndex: 2]; Quarto [buttonImage objectAtIndex: 3]; Similario è necessario impostare – Sumanth

+0

: Tks man ... Ho ottenuto il mio risultato definendo objectAtIndex come indice ... Ma ho provato a sparare azioni diverse per ogni pulsante selezionandolo ... Ecco il mio codice per il primo pulsante – Dany

2

Non è possibile impostare una serie di immagini sulla proprietà dell'immagine UIButton. Se si guarda la documentazione di UIButton si vede che si aspetta uno UIImage.

1

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

+0

: grazie mille amico ... funziona ... – Dany

2

Qual è l'indice nel tuo codice ?? Impostare questo:

[button setBackgroundImage:[buttonImage objectAtIndex:index] forState:UIControlStateNormal]; 
Problemi correlati