动态生成的UIButton单击时出现EXC_BAD_ACCESS错误

时间:2012-06-04 07:47:22

标签: ios5 xcode4.3

我编写了以下代码,以在滚动视图中添加可选缩略图列表

while(element = [enumerator nextObject])
    {
        // Do your thing with the object.
        MenuUrl *menuUrl = (MenuUrl *) element;
        if([menuUrl.index isEqualToString:@"menu_url"] || [menuUrl.index isEqualToString:@"photos_url"])
            continue;
        //NSLog(@"%@", menuUrl.index);
        NSString *url = menuUrl.image.thumbUrl;

        dispatch_queue_t download_queue = dispatch_queue_create("image downloader", NULL);
        dispatch_async(download_queue, ^{
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: url]]];
            CGRect imageframe;
            imageframe.origin.x = 15+ 101.6*(imageNumber % 3);
            //NSLog(@"%d", imageNumber);
            imageframe.origin.y =  101.6*(imageNumber / 3);
            imageframe.size.height = 86.6;
            imageframe.size.width = 86.6;
            dispatch_async(dispatch_get_main_queue(), ^{

                UIButton *myButton0 = [[UIButton alloc] initWithFrame:imageframe];
                myButton0.tag = imageNumber;
                [myButton0 setBackgroundImage:image forState:UIControlStateNormal];
                [myButton0 addTarget:self action:@selector(showDetailImage) forControlEvents:UIControlEventTouchUpInside];
                [scrollView addSubview:myButton0];

            });
        });
        dispatch_release(download_queue);
        imageNumber = imageNumber + 1;


    }
    [self.view addSubview:scrollView];

现在,每当我选择任何缩略图时,我都会收到EXC_BAD_ACCESS错误。 为什么会这样?

0 个答案:

没有答案