2013-04-27 65 views
0

Goodevening不能添加的UIButton到的UIScrollView

雖然我讀過有關添加一個UIButton到的UIScrollView它並沒有爲我工作了很多相關的問題。當我使用UIImages而不是UIButtons時,它就像一個魅力。我在這裏錯過了什麼? (不介意,如果公司或for循環,人的是正確的,圖片的路徑也)

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); 
      } 

     } 
    } 

乾杯

+1

在[fotoButton setImage:[UIImage imageNamed:path] forState:UIControlStateNormal]; UIImage imageNamed部分不正確,請嘗試添加此[UIImage imageWithData:[NSData dataWithContentsOfFile:path]; – 2013-04-27 17:18:50

+0

你剛剛度過了我的一天!謝謝! – 2013-04-27 17:30:22

回答

1

[UIImage imageNamed:path]看起來不正確, 嘗試[UIImage imageWithData:[NSData dataWithContentsOfFile:path]


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

這確實是錯誤。 Thilina Hewagama剛纔也這麼說。不管怎麼說,還是要謝謝你! – 2013-04-27 17:36:30