我正在尝试在我的项目中实现页面滚动功能。我为 iOS 5 开发,使用 Storyboard(和 ARC)。这就是我在 Storyboard中所做的:
第一个 viewController(左边)有一个 scrollView 和一个 pageControl。它的类叫做GlobalDashboardViewController
,继承自类DashboardViewController
(它继承自UIViewController
)。其他 2 个 Controller 是带有标识符的简单 UIViewController(MainDashboard 和 SecondaryDashboard)。
在GlobalDashboardViewController.m
中,只有一个viewDidLoad,获取childViewControllers:
- (void)viewDidLoad
{
[super viewDidLoad];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"MainDashboard"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"SecondaryDashboard"]];
}
DashboardViewController.m
有点复杂。以下是最重要的方法:
- (void)viewDidLoad
{
// Calling the viewDidLoad above to populate the childViewControllers array
[super viewDidLoad];
[self.scrollView setPagingEnabled:YES];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
[self.scrollView setShowsVerticalScrollIndicator:NO];
[self.scrollView setDelegate:self];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.pageControl.currentPage = 0;
_page = 0;
[self.pageControl setNumberOfPages:[self.childViewControllers count]];
UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
if (viewController.view.superview != nil) {
[viewController viewWillAppear:animated];
}
self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height);
}
还有很多其他方法,但我认为它们不相关,因为这是我的问题:应用程序启动后,我看到了 scrollView 和 pageControl。背景为深灰色,正如我在 GlobalDashboard viewController 中设置的那样。我可以正确滚动,并且 pageControl 已更新。但我看不到其他观点。当我在 viewWillAppear
中的 if 处放置断点并查看 viewController
时,它的名称正确(Dashboard Page 1
,这是我在 Storyboard中给 Controller 的名称),但它的 _view
属性显示 0x00000000,如您在此处看到的:
所以,我永远不会进入 if,这是实际显示 viewController 的一些魔法发生的地方......
我的工作基于一个示例项目,您可以在此处找到:PageViewController .当我运行它时,它运行良好。我不知道为什么我不能让它在我自己的项目中工作。
有什么想法吗?
谢谢
最佳答案
您永远不会添加 View 本身([self.view addSubview:/*您的子 Controller 的 View */]
)。您链接到的项目在 PagerViewController.m 内的 - (void)loadScrollViewWithPage:(int)page
中执行此操作
关于ios - viewController.view.superview 为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12578974/