2013-04-27 1 views
0

GoodeveningImpossible d'ajouter UIButton à UIScrollView

Bien que j'ai lu pas mal de questions liées au lot sur l'ajout d'un UIButton à un UIScrollView il ne fonctionne pas pour moi. Quand j'utilise UIImages à la place de UIButtons cela fonctionne comme un charme. Qu'est-ce que j'oublie ici? (Ne me dérange pas les cas de ou boucles, al ceux sont corrects, les chemins d'images aussi)

for(int j = 0; j < [subcategorie count]; j++){ 

     NSArray *product = [subcategorie objectAtIndex:(j)]; 
     int posX = 0; 
     for(int k = 1; k <= [product count]; k++){ 

      if((i-1) == currentCategory){ 
       //NSString *fotoName = [NSString stringWithFormat:@"images/product/%@/%@_%i.png",self.categorieen[(i-1)] ,self.categorieen[(i-1)] , k]; 

       NSString *fotoName = [NSString stringWithFormat:@"%@_%i",self.categorieen[(i-1)] , k]; 

       NSString *currentCategory = [NSString stringWithFormat:@"images/product/%@/", self.categorieen[(i-1)]]; 
       NSString *path = [[NSBundle mainBundle] pathForResource:fotoName ofType:@"png" inDirectory:currentCategory]; 

       UIButton *fotoButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
       [fotoButton setImage:[UIImage imageNamed:path] forState:UIControlStateNormal]; 
       fotoButton.frame = CGRectMake(posX, 252, 718, 520); 
       fotoButton.backgroundColor = [UIColor clearColor]; 

       self.scrollview.contentSize = CGSizeMake(posX, 252); 
       [self.scrollview setBounces:YES]; 
       [self.scrollview setPagingEnabled:YES]; 
       [self.scrollview addSubview:fotoButton]; 
       [self.scrollview bringSubviewToFront:fotoButton]; 
       [self.scrollview setUserInteractionEnabled:YES]; 

       self.scrollview.userInteractionEnabled = YES; 
       [self addSubview: self.scrollview]; 
       posX += 718; 

       NSLog(@"Fotolink = %@", fotoName); 
      } 

     } 
    } 

Vive

+1

dans [fotoButton setImage: [UIImage imageNamed: chemin] forState: UIControlStateNormal]; La pièce UIImage imageNamed est incorrecte, essayez d'ajouter ceci [UIImage imageWithData: [NSData dataWithContentsOfFile: path]; –

+0

Vous venez de faire ma journée! Merci! –

Répondre

1

[UIImage imageNamed:path] semble incorrecte, essayer [UIImage imageWithData:[NSData dataWithContentsOfFile:path]


UIButton *fotoButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[fotoButton setImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:path] forState:UIControlStateNormal]; 
+0

C'était en effet la faute. Thilina Hewagama vient de dire la même chose. Merci quand même! –

Questions connexes