在我的應用程序我使用滾動查看(分頁)。滾動視圖(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;
可以看到按鈕,但它不是所需的座標。所以爲什麼「我」不能工作?我是否做錯了什麼或缺少什麼?
什麼是我的計算,如果你用的斷點檢查的價值? –
在float y = tagY後嘗試NSLog(@「1024 *%i +%f =%f」,i,tagX,1024 * i + tagX) –
我用斷點檢查,值從0改變爲圖片數量。 – user1938695