问题描述
|
我从apple示例项目滚动中获得了以下代码:
@synthesize scrollView1,scrollView2;
const CGFloat kScrollObjHeight = 467.0;
const CGFloat kScrollObjWidth = 320.0;
const NSUInteger kNumImages = 6;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc,0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth),[scrollView1 bounds].size.height)];
}
我试图修改它以加载子视图而不是图像,但是它不起作用。我想以某种方式替换视图而不是添加视图?我需要从单独的xib文件加载视图,并在xib \的左右滚动(它们每个都包含一个导致游戏中不同级别的按钮面板)。那我在做什么错?为什么不起作用?它只显示第二个面板,每当我按下按钮时,它就会因SIGABRT而崩溃。
#import \"LevelSelectButtons.h\"
@implementation LevelSelectButtons
@synthesize levelScroll;
const int kScrollObjHeight = 400;
const int kScrollObjWidth = 320;
const NSUInteger kNumCategories = 5;
- (void)layoutScrollScreens {
UIImageView *view = nil;
NSArray *subviews = [levelScroll subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews) {
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) {
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc,0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[levelScroll setContentSize:CGSizeMake((kNumCategories * kScrollObjWidth),[levelScroll bounds].size.height)];
}
- (void)viewDidLoad {
[levelScroll setScrollEnabled:YES];
CGFloat width = 640; //kScrollObjWidth*kNumCategories;
CGFloat height = 400;
[levelScroll setContentSize:CGSizeMake(width,height)];
[self.view addSubview:levelScroll];
levelScroll.pagingEnabled = YES;
levelScroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
levelScroll.pagingEnabled = YES;
/// LEVELS 1 ///
NSUInteger viewNum = 0;
CGRect catViewFrame = CGRectMake(0,kScrollObjWidth,kScrollObjHeight);
UIView *levels1view = [[UIView alloc] initWithFrame:catViewFrame];
NSArray *array1 = [[NSBundle mainBundle] loadNibNamed:@\"Levels1\" owner:self options:nil];
levels1view = [array1 objectAtIndex:0];
levels1view.tag = viewNum;
[self.levelScroll addSubview:levels1view];
/// LEVELS 2 ///
viewNum = 1;
catViewFrame = CGRectMake(320,kScrollObjHeight);
UIView *levels2view = [[UIView alloc] initWithFrame:catViewFrame];
NSArray * array2= [[NSBundle mainBundle] loadNibNamed:@\"Levels2\" owner:self options:nil];
levels2view = [array2 objectAtIndex:0];
levels2view.tag = viewNum;
[self.levelScroll addSubview:levels2view];
[self layoutScrollScreens];
[super viewDidLoad];
}
这是SIGABRT出现的行:
int retVal = UIApplicationMain(argc,argv,nil,nil);
解决方法
这是为了解释为什么只显示“ 3”。
/// LEVELS 2 ///
viewNum = 1;
catViewFrame = CGRectMake(320,kScrollObjWidth,kScrollObjHeight);
UIView *levels2view = [[UIView alloc] initWithFrame:catViewFrame];
如果您在此处查看,则说明您正在使用所需的框架创建新视图,您要在其中放置levels2view
。但,
NSArray * array2= [[NSBundle mainBundle] loadNibNamed:@\"Levels2\" owner:self options:nil];
levels2view = [array2 objectAtIndex:0];
levels2view.tag = viewNum;
[self.levelScroll addSubview:levels2view];
在这里,您正在将变量重新分配给新视图,从而丢失了对您创建的视图的引用,从而导致内存泄漏。顺便说一句,这个视图没有框架。默认将其原点设为CGPointZero
。而且,由于您对levels1view
执行相同的操作,因此当按顺序将levels2view
添加到滚动视图时,最终会将levels2view
定位在levels1view
上方。
为了解决这个问题,
viewNum = 1;
catViewFrame = CGRectMake(320,kScrollObjHeight);
NSArray * array2= [[NSBundle mainBundle] loadNibNamed:@\"Levels2\" owner:self options:nil];
UIView * levels2view = [array2 objectAtIndex:0];
levels2view.frame = catViewFrame;
levels2view.tag = viewNum;
[self.levelScroll addSubview:levels2view];