0

所以我有一个滚动视图,其中有 3 个其他视图作为子视图。使用滚动视图在 3 个子视图之间进行分页,这些子视图的控制器存储在数组中。

- (void)viewDidLoad
{  
    [self loadData];

    self.uiScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    [self.uiScrollView setPagingEnabled:YES];
    [self.uiScrollView setContentSize:CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height)];

    self.uiScrollView.delegate = self;


    [self.view addSubview:self.uiScrollView];

    self.uiPhotoCollectionViewControllers = [[NSMutableArray alloc] init];
    int numberOfOtherViews = 3;
    int subViewOffset = 0;

    for (int i = 0; i < numberOfCollectionViews; i++)
    {
        UIViewController* uiViewController = [[UIPhotoCollectionViewController alloc] initWithData:dataArray];
        // add offset to the view of the controller
        [self.uiScrollView addSubview:uiViewController.view];
        [self.uiViewControllers addObject:uiViewController];
        uiViewController.view.frame = CGRectOffset(uiViewController.view.frame,subViewOffset,0.0f);
        subViewOffset = subViewOffset + uiViewController.view.frame.size.width;
    }

如果我以后想用第一个视图切换滚动视图中间的视图,我将如何在不删除所有滚动视图子视图并替换它们的情况下进行此操作。

4

1 回答 1

0

不替换是不可能的,但是您可以通过像这样交换 self.uiViewControllers 数组中的对象来做到这一点

[scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

然后在交换对象后,您可以通过遍历整个数组来添加子视图。

于 2013-10-09T04:57:23.013 回答