我希望这是有道理的......我有一个 NSMutableArray,我试图在其中存储多个 UIScrollView。每个 UIScrollView 将有多个图像,最终目标是能够允许用户垂直滑动类别(每个UIScrollView 的实例)和该类别中的每个图像(UIImageView 的每个实例)的水平。现在,在我让这段代码工作之前,我只是用 1 个图像创建 1 个 UIScrollView,并将其添加到 scrollViewManager。这似乎正确添加了所有内容,但是当我离开此功能时,游戏结束了。我的数组是空的。我不明白。当我添加到数组时,我是否应该进行某种深拷贝,以免它被破坏。也许我明天醒来时会弄清楚,但现在,我想打破眼前的一切。谢谢,
编辑:抱歉缺少细节,我发布这个很晚。我的项目正在使用 ARC,所以我不会在任何地方发布 scrollView。这是我的头文件中 scrollViewManager 的声明:
@property (nonatomic, retain) NSMutableArray *scrollViewManager;
这是我用来检索我的滚动视图的代码:
UIScrollView *test = (UIScrollView*) [self.scrollViewManager objectAtIndex: 0];
这是初始化滚动视图和数组的代码:
scrollViewManager = [[NSMutableArray alloc] initWithCapacity: 1];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSString *imageName = @"pic1.png";//[NSString stringWithFormat:@"pic%d.jpg", i+1];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = 400;
rect.size.width = 300;
imageView.frame = rect;
imageView.tag = 1; // tag our images for later use when we place them in serial fashion
[scrollView addSubview:imageView];
[self.scrollViewManager addObject: scrollView];