检查是否已创建多个UIScrollViews

时间:2018-07-18 15:04:08

标签: ios objective-c uiscrollview

我的UIScrollView和API调用后出现问题,我想知道在导航到viewController时是否有一种方法可以检查正在加载多少scrollViews。

这是我用来为viewController设置滚动视图的代码

[self.imageScroller removeFromSuperview];
[self.seperatorView removeFromSuperview];
[self.containerView removeFromSuperview];
[self.pagedScrollViewController willMoveToParentViewController:nil];
[self.pagedScrollViewController.view removeFromSuperview];
[self.pagedScrollViewController removeFromParentViewController];
[self.pagedScrollViewController didMoveToParentViewController:nil]



self.pagedScrollViewController = [[PagedScrollViewController alloc]initWithModel:self.model];
[self addChildViewController:self.pagedScrollViewController];
self.pagedScrollViewController.view.frame = self.containerView.frame;
[self.containerView addSubview:self.pagedScrollViewController.view];
[self.pagedScrollViewController didMoveToParentViewController:self];

我希望能够跟踪滚动视图被添加了多少次,任何想法将不胜感激。

3 个答案:

答案 0 :(得分:0)

使用subviews返回视图中的子视图数组。

let subview = self.view.subviews
for view in subview {
      if view is UIScrollView {
          // your code her
      }
}

对于目标c

NSArray *arraySubView = [self.view subviews];
for (UIView *subView in arraySubView) {
    if ([subView isKindOfClass: [UIScrollView class]]) {
        //your code
    }
}

答案 1 :(得分:0)

您可以在设置所有视图后添加它:

int scrollViewCount = 0;
for (UIView *view in self.view.subviews) {
    if([view isKindOfClass:[UIScrollView class]]) {
        scrollViewCount += 1;
    }
}

答案 2 :(得分:-1)

向您添加2行viewControllers viewDidAppear函数

override func viewDidAppear(_ animated: Bool) {
    print("ChildViewController: ", self.childViewControllers)
    print("Subviews: ", self.view.subviews)
}

您应该在控制台输出中看到视图控制器和视图的列表。

检查多少个scrollView实例。


或者,一旦加载了viewController,请在视图层次结构检查器中查看:

View Heirarchy inspector

Example