2013-07-28 75 views
0

在我的應用程序我使用滾動查看(分頁)。滾動視圖(UIImageView)中的每個視圖都包含一個圖像,每個圖像都有標籤(按鈕)散佈在整個圖片上。所以這裏是我的代碼:UIButton無法顯示

for (int i = 0; i < self.pictures.count; i++) 
    { 
     //The first loop is to loop to every picture 
     int nbOfTags = classObject.tagsImages.count; 

     for (int j = 0; j < nbOfTags; j++) 
     { 
      //Second loop is to loop to each tag corresponding to the picture 

      ListTags *listTags = [[ListTags alloc]init]; 

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

      listTags = [tagInfo objectAtIndex:j]; 

      float tagX = [listTags.tag_x floatValue]; 
      float tagY = [listTags.tag_y floatValue]; 
      float x = 1024*i+ tagX; 
      float y = tagY; 

      CGRect rect = CGRectMake(x, tagY, 20, 20); 
      UIButton *button = [[UIButton alloc] initWithFrame:rect]; 

      UIImage * buttonImage = [UIImage imageNamed:@"blueCircle.png"]; 

      [button setBackgroundImage:buttonImage forState:UIControlStateNormal]; 


      [self.scrollView addSubview:button]; 
     } 
    } 

問題:當我運行代碼時,我看不到圖片上的按鈕(標籤)。如果我用「j」代替「i」在

float x = 1024 * i + tagX;

可以看到按鈕,但它不是所需的座標。所以爲什麼「我」不能工作?我是否做錯了什麼或缺少什麼?

+0

什麼是我的計算,如果你用的斷點檢查的價值? –

+0

在float y = tagY後嘗試NSLog(@「1024 *%i +%f =%f」,i,tagX,1024 * i + tagX) –

+0

我用斷點檢查,值從0改變爲圖片數量。 – user1938695

回答

0

在界面構建器(或故事板 - IB)中取消選中自動佈局(如果選中),那麼您的按鈕將顯示在你想要的位置。

+0

代碼有更多的問題比這可能造成的.... – lnafziger

0

看到我的評論在下面的代碼:

for (int i = 0; i < self.pictures.count; i++) 
    { 
     //The first loop is to loop to every picture 
     /* 
     * This will give you the same number of tags for every image. 
     * Is that what you want? 
     */ 
     int nbOfTags = classObject.tagsImages.count; 

     for (int j = 0; j < nbOfTags; j++) 
     { 
      //Second loop is to loop to each tag corresponding to the picture 

      /* This allocates a brand new ListTags object EVERY time through the loop. */ 
      ListTags *listTags = [[ListTags alloc]init]; 

      /* This allocates a brand new EMPTY array EVERY time through the loop. */ 
      NSMutableArray *tagInfo = [[NSMutableArray alloc]init]; 

      /* 
      * This discards the new ListTags object that you just created two lines ago 
      * and replaces it with an object from tagInfo. Which doesn't exist because 
      * you just created a new array in the previous line. This really should 
      * crash, which leads me to believe that this isn't the actual code that you 
      * are using. Post the actual code that you are using so that we can help you 
      * better. 
      */ 
      listTags = [tagInfo objectAtIndex:j]; 

      /* None of this is valid since listTags is nil/we never should have gotten here. */ 
      float tagX = [listTags.tag_x floatValue]; 
      float tagY = [listTags.tag_y floatValue]; 
      float x = 1024*i+ tagX; 
      float y = tagY; 

      CGRect rect = CGRectMake(x, tagY, 20, 20); 
      UIButton *button = [[UIButton alloc] initWithFrame:rect]; 

      UIImage * buttonImage = [UIImage imageNamed:@"blueCircle.png"]; 

      [button setBackgroundImage:buttonImage forState:UIControlStateNormal]; 


      [self.scrollView addSubview:button]; 
     } 
    }