我正在尝试以编程方式在子视图中创建页面视图。但是,当我将UIPageViewController的dataSource设置为superviewcontroller时,我收到此错误:
-[UIView scrollView]: unrecognized selector sent to instance 0x7fc223ea8300
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView scrollView]: unrecognized selector sent to instance 0x7fc223ea8300'
这是我的代码:
import UIKit
class MainVC: UIViewController, UIPageViewControllerDataSource {
private var pages: [UIViewController]
private let pageVC: UIPageViewController
init() {
pages = [UIViewController]()
for i in 0...1 {
let vc = UIViewController()
vc.view = MyUIViewSubclass(index: i)
pages.append(vc)
}
pageVC = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
pageVC.view = UIView()
pageVC.view.translatesAutoresizingMaskIntoConstraints = false
super.init(nibName: nil, bundle: nil)
pageVC.dataSource = self //Breakpoints indicate that this is when the exception occurs
view.addSubview(pageVC.view)
//...Autolayout...
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
return pages[0]
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
return pages[1]
}
}
提前致谢。
答案 0 :(得分:0)
pageVc.view有一个名为scrollview的ibaction未在视图控制器中声明,您需要从连接检查器中删除ibaction
答案 1 :(得分:0)
好的,所以我遇到了与您相同的问题,并且发现了为什么会这样。
由于您在pageVC.view = UIView()
行所在的行而发生错误。
UIPageViewController的视图具有滚动视图,在其中可以进行VC之间的滚动魔术。通过为其设置一个空的UIView,您将其删除,从而使其无法工作。如果您想更改其视图的外观,则应直接进行操作,而不要设置其他视图。