我是第一次使用UIPageViewController
。当然是iOS5,而且我对ARC很满意。我不使用 Storyboard。
在我看到的所有零散示例中,UIPageViewController
从未“拥有”屏幕。它始终是其他 View Controller 的子级。这是一个要求吗?我想让它成为我的应用程序的根 Controller ,就像通常使用 UINavigationController
所做的那样。我试图以编程方式将其全部设置,但我得到的只是一个空的页面 View Controller ,其中没有任何内容。
以下是应用程序的启动方式(此处的所有代码都在我的应用程序委托(delegate)类中):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self configureLoggers];
[self applyAppearanceProxySettings];
[self configureDataStore];
[self configureNavigationController];
[self configurePageController]; // <-- this is where we do the page view controller setup, shown next.
self.window.rootViewController = self.pageController;
[self.window makeKeyAndVisible];
log4Info(@"Application completed launching.");
return YES;
}
上面代码中提到的方法,用于设置页面 View Controller :
- (void)configurePageController {
CCFrontCoverViewController *frontCoverViewController = [[CCFrontCoverViewController alloc] initWithNibName:nil bundle:nil];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMin]
forKey:UIPageViewControllerOptionSpineLocationKey];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:options];
self.pageController.delegate = self;
self.pageController.dataSource = self;
self.pageController.view.backgroundColor = [UIColor redColor]; // So we know we're missing content because red shows through.
NSArray *pageViewControllers = [NSArray arrayWithObjects:frontCoverViewController, self.navigationController, nil];
[self.pageController setViewControllers:pageViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
我还在两个数据源回调方法中添加了日志记录,但它们甚至没有被调用。
当它运行时,我只是看到一个空白的红色屏幕。再说一次,没有涉及 Storyboard。我错过了什么?
最佳答案
我可以使用 UIPageViewController
作为示例项目中的 Root View Controller ,前提是:
UIPageViewController
已使用有效的书脊位置正确初始化。UIPageViewController
的doubleSided
属性已设置(与书脊位置一致)。UIPageViewController
的viewControllers
数组已设置(再次与书脊位置和doubleSided
属性一致)。
看起来您尚未在代码中将 doubleSided
设置为 YES
(因为您的 viewControllers
数组中有两个项目)。在设置 viewControllers 数组之前添加该内容是否可以解决您的问题?
编辑:仅供后代使用,在UIPageViewController docs中setViewControllers:direction:animation:completion:
方法的描述中有一个图表,显示了脊柱位置和 doubleSided
的所有有效组合所允许的 View Controller 数量。
关于objective-c - UIPageViewController 作为应用程序的根 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8839670/