我创建了一个简单的窗口,目的是成为一个类似“巫师”的东西(我知道苹果的指导方针基本上禁止你这样做,我试图说服客户,但不管怎样。
这只是一个简单的视图,里面有两个自定义视图,其中一个在底部,其中一个包含“上一个”和“下一个”按钮,而顶部的一个较大的视图占据了大部分空间。
我将底部视图称为"NavigationView“,将顶部视图称为"ContainerView”。
我创建了一个数组来保存用户应该使用“下一步”和“上一步”按钮浏览的一系列视图。
所以,这是我的代码。
- (IBAction) next:(id)sender{
currentViewIndex++;
[self animatePushView:YES];
}
- (IBAction)previous:(id)sender{
currentViewIndex--;
[self animatePushView:NO];
}
- (void) animatePushView:(BOOL)forward{
NSView *nextView = [viewCollection objectAtIndex:currentViewIndex];
for (NSView *subView in [containerView subviews]) {
[subView removeFromSuperview];
}
[containerView addSubview:nextView];
[nextView setFrame:containerView.bounds];
[containerView setNeedsDisplay:YES];
}
我认为这很简单。我有一个数组,其中包含要显示的下一个视图。
实际情况是,我发现下一个视图位于ContainerView的左下角。这一切为什么要发生?
此外,正如你可能已经猜到的那样,我是一个管理视图的新手,尽管我已经在objective-c上工作了很长一段时间,所以如果我错过了一些最佳实践,我愿意接受建议。谢谢!
编辑:
我忘了补充:其中一些视图有不同的大小,我希望能够根据视图大小更改窗口大小。
发布于 2013-05-10 09:28:06
好了,我想通了..终于..。
问题是我必须显示的第一个视图已经包含在.xib文件的容器视图中。
我真的不知道为什么,但这可能会导致容器视图的保留计数出现问题,因为它是在第一次单击时释放的。释放容器视图将重新定位(0,0)上的视图,可能是因为它的框架为空,并且视图将闪烁,因为它没有正确保留。
从.xib文件中删除视图并通过代码添加它无论如何都能正常工作。
发布于 2013-05-09 11:29:29
[nextView setFrame:containerView.bounds];
您正在将容器视图边界指定给下一个图幅(doc)。
您可能需要将当前图幅指定给下一个图幅,并可能需要调整宽度和高度。
保持对当前显示视图的引用,如下所示(_currentView是NSView *
类型的ivar ):
- (IBAction) next:(id)sender{
currentViewIndex++;
[self animatePushView:YES];
}
- (IBAction)previous:(id)sender{
currentViewIndex--;
[self animatePushView:NO];
}
- (void) animatePushView:(BOOL)forward{
NSView *nextView = [viewCollection objectAtIndex:currentViewIndex];
[nextView setFrame:_currentView.frame];
[_currentView removeFromSuperview]; // _currentView is retained in the collection
[containerView addSubview:nextView];
_currentView = nextView;
[containerView setNeedsDisplay:YES];
}
https://stackoverflow.com/questions/16459674
复制相似问题