添加到NSArray的UIImageView没有响应

时间:2012-03-01 13:12:37

标签: objective-c ios xcode uiimageview

我是Objective-C的新手,这是我第一次真的不知道该怎么做。

我有一个班级 - Parser - 用图片创建UIImageViewsinit,然后将它们添加到UIView该课程还会将这些UIImageViews添加到NSMutableArray - imgArray,这是另一个类的属性 - Page

然后,点击一个按钮,我从Page类调用Manager类,循环遍历imgArray并尝试为{{1}设置新图像在数组中。

它不起作用。实际上,我对UIImageViews所做的一切都不起作用。 我尝试UIImageViews[img removeFromSuperView]等等。它没有回复。

这里我在Page:

中声明了Array属性
[img setHidden:YES]

以下是Parser的代码,我在其中创建图像并将其添加到数组中:

@property (nonatomic, retain) NSMutableArray *imgArray;

以下是Manager的循环代码:

UIImageView *img = [[UIImageView alloc] initWithFrame: frame];
        NSString *name = [c imageSource];
        [img setImage: [UIImage imageFromBook: name]];
        [view addSubview: img];
        [c setImage:img];
        if (!page.imgArray)
        {
            page.imgArray = [[NSMutableArray alloc] init];
        }
        [page.imgArray addObject:img];
        [img release];

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

无需将UIImageView存储在数组中。

创建UIImageView时,可以使用任意数字标记它们,例如。

#define K_MIN_IMGV 100
#define K_MAX_IMGV 120

- (void) setupViews
{
    for (NSInteger count = K_MIN_IMGV; count < K_MAX_IMGV; ++count)
    {
        UIImageView *imgV = // create image view, set properties
        imgV.tag = count;   // tag the views for easy retrieval later
        ...
        [mainView addSubview: imgV]; // add subviews
        [imgV release], imgV = nil;  // mainView now manages, release our allocation
    }
}    

//如何设置新图片

- (void) setupNewImages
{
    for (NSInteger count = K_MIN_IMGV; count < K_MAX_IMGV; ++count)
    {
        UIImageView *imgV = (UIImageView *) [mainView viewWithTag: count]; // retrieve imageView
        [imgV setImage: newImage]; // set new image
    }
} 

//要删除imageView,您可以使用

UIImageView *imgV = (UIImageView *) [mainView viewWithTag: 123];
[imgV removeFromSuperview];

答案 1 :(得分:0)

您是否尝试使用以下方法枚举数组:

for(UIImageView* imageView in page.imgArray)
{
 //Do code
}

这将告诉你page.imgArray中是否有任何imageViews。

如何向NSMutableArray添加对象以及如何初始化NSMutableArray。 我记得在向NSMutableArray添加对象时遇到问题,但设置我的init来解决这个问题:

NSMutableArray* mutableArray = [NSMutableArray array];

您还在页面中的imgArray上设置了哪些属性?你保留这些价值吗?

需要查看更多代码才能获得更全面的理解。

相关问题