我第一次尝试让 UIPageViewController 工作,但每当我尝试设置它的 View Controller 时,我似乎都遇到了麻烦。
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let leftViewController = storyboard?.instantiateViewControllerWithIdentifier("LeftViewController")
let rightViewController = storyboard?.instantiateViewControllerWithIdentifier("RightViewController")
let viewControllerArray: [UIViewController] = [leftViewController!, rightViewController!]
self.setViewControllers(viewControllerArray, direction: .Forward, animated: true, completion: nil)
}
每次我到达 setViewControllers()
方法时,应用程序都会在 AppDelegate
文件中崩溃,并显示以下错误日志:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'The number of view controllers provided (2) doesn't match the number
required (1) for the requested transition
23 UIKit 0x0000000103a06610 UIApplicationMain + 171
24 PageViewTesting 0x000000010300649d main + 109
25 libdyld.dylib 0x0000000105a3292d start + 1
26 ??? 0x0000000000000001 0x0 + 1)
libc++abi.dylib: terminating with uncaught exception of type NSException
目前我的 Storyboard中有一个 UIPageViewController
和两个 UIViewControllers
。我已经仔细检查过它们的 ID 对于 instantiateViewControllerWithIdentifier()
方法也是正确的。
关于我收到此错误的原因有什么想法吗?
最佳答案
来自 UIPageViewController
的类引用:
// setViewControllers:direction:animated:completion:
func setViewControllers(_ viewControllers: [UIViewController]?,
direction direction: UIPageViewControllerNavigationDirection,
animated animated: Bool,
completion completion: ((Bool) -> Void)?)
The view controllers passed to this method are those that will be visible after the animation has completed. Use a data source to provide additional view controllers to which users navigate.
对于单 View Controller View 模式,代码中使用的方法.setViewControllers
只需要一个 View Controller (但是,在数组形式上)。参数采用数组 [UIViewController]
有点令人困惑,但这是为了在使用 spine(中间) View 时使用 PageViewController 的 doubleSided
属性。
When defining a page view controller interface, you can provide the content view controllers one at a time (or two at a time, depending upon the spine position and double-sided state)
...
UIPageViewControllerSpineLocationMid
Spine in the middle or the screen.
Two view controllers are displayed at a time.
您打算使用书脊中 View 吗?在这种情况下,您可以发送两个 View Controller ,否则,您应该只发送一个并使用数据源添加其他 View Controller 。
关于ios - 如何防止 UIPageViewController 的 setViewControllers 方法崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34466847/