0

在我的应用程序中,我使用的是滚动视图(分页)。滚动视图 (UIImageView) 中的每个视图都包含一个图像,每个图像都有散布在图片上的标签(按钮)。所以这是我的代码:

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

问题:当我运行代码时,我看不到图片上的按钮(标签)。如果我将“i”替换为“j”

浮动 x = 1024*i+ 标记X;

可以看到按钮,但它不是所需的坐标。那么为什么“我”不能工作?我做错了什么或错过了什么吗?

4

2 回答 2

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];
        }
    }
于 2013-07-28T23:11:47.373 回答
0

在界面生成器(或故事板 - IB)中取消选中自动布局(如果已选中),那么您的按钮将显示在您想要的位置。

于 2013-07-28T22:36:25.180 回答