2013-07-28 135 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]; 
     } 
    }