在运行时从 UIScrollView/PageControl 中删除视图/控制器

发布于 2024-10-20 01:36:50 字数 1833 浏览 2 评论 0原文

我使用滚动视图和页面控件来渲染多个图像,与内置照片应用程序非常相似。我使用此代码在视图加载时设置控制器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-10-27 01:36:50

因此,您希望在更新 scrollView 时确保 scrollView 的子视图与 self.viewControllers 中的项目保持同步?最简单的事情是每当您更换其中一项时执行以下操作:

UIViewController *vc = [self.viewControllers objectAtIndex:i];
if ((NSNull *)vc != [NSNull null] && [vc isViewLoaded]) [vc.view removeFromSuperview];
[self.viewControllers replaceObjectAtIndex:i withObject:foo];

So you want to make sure the subviews of scrollView stay in sync with the items in self.viewControllers as you update the latter? The easiest thing is to do something like this whenever you replace one of the items:

UIViewController *vc = [self.viewControllers objectAtIndex:i];
if ((NSNull *)vc != [NSNull null] && [vc isViewLoaded]) [vc.view removeFromSuperview];
[self.viewControllers replaceObjectAtIndex:i withObject:foo];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文