在运行时从 UIScrollView/PageControl 中删除视图/控制器
我使用滚动视图和页面控件来渲染多个图像,与内置照片应用程序非常相似。我使用此代码在视图加载时设置控制器:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [photos count]; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
scrollArea.pagingEnabled = YES;
scrollArea.contentSize = CGSizeMake(newW * [photos count], newH);
scrollArea.showsHorizontalScrollIndicator = NO;
scrollArea.showsVerticalScrollIndicator = NO;
scrollArea.scrollsToTop = NO;
scrollArea.delegate = self;
pageControl.numberOfPages = [photos count];
newW 和 newH 只是我用来跟踪方向变化时的大小的两个变量。 我的 loadScrollViewWithPage 方法:
- (void) loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [photos count]) return;
PhotoViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[PhotoViewController alloc] init];
controller.imgPath = (NSString *)[photos objectAtIndex:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (nil == controller.view.superview) {
CGRect frame = scrollArea.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollArea addSubview:controller.view];
}
}
这是我的问题。我需要在需要时卸载控制器/视图。我想我可以使用这样的东西:
for (unsigned i = 0; i < [photos count]; i++) {
[self.viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
我想这样做,所以将全新的视图加载到滚动视图中。
我也尝试过类似的事情,但没有运气:
for(UIView *subview in [self.scrollArea subviews]) {
[subview removeFromSuperview];
}
有什么想法吗?
I'm using a scrollview and a pagecontrol to render several images, very similar to the built-in photo app. I'm using this code to setup the controllers when the view loads:
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < [photos count]; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
scrollArea.pagingEnabled = YES;
scrollArea.contentSize = CGSizeMake(newW * [photos count], newH);
scrollArea.showsHorizontalScrollIndicator = NO;
scrollArea.showsVerticalScrollIndicator = NO;
scrollArea.scrollsToTop = NO;
scrollArea.delegate = self;
pageControl.numberOfPages = [photos count];
newW and newH are just two variables I'm using to keep track of the size when the orientation changes.
My loadScrollViewWithPage method:
- (void) loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= [photos count]) return;
PhotoViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[PhotoViewController alloc] init];
controller.imgPath = (NSString *)[photos objectAtIndex:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
if (nil == controller.view.superview) {
CGRect frame = scrollArea.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[scrollArea addSubview:controller.view];
}
}
And here is my issue. I need to unload controllers/views when I need it. I thought I could use something like this:
for (unsigned i = 0; i < [photos count]; i++) {
[self.viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
I want to do it, so load completely new views into the scrollview.
I also tried something liek this with no luck:
for(UIView *subview in [self.scrollArea subviews]) {
[subview removeFromSuperview];
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,您希望在更新
scrollView
时确保scrollView
的子视图与self.viewControllers
中的项目保持同步?最简单的事情是每当您更换其中一项时执行以下操作:So you want to make sure the subviews of
scrollView
stay in sync with the items inself.viewControllers
as you update the latter? The easiest thing is to do something like this whenever you replace one of the items: