所以我是iOS开发的新手。我正在尝试制作页面滑动功能,如图像轮播或类似应用教程,我正在遇到线程1:信号SIGABRT错误。在日志中是状态'无法将类型'ViewController'的值转换为'UIPageViewController'
不确定您是否需要整个代码。
我的问题在于:
self.pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "contentViewController") as! UIPageViewController
帮助将不胜感激。
class ViewController: UIViewController, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController!
var pageTitles: NSArray!
var pageImages: NSArray!
override func viewDidLoad() {
self.pageTitles = NSArray(objects: " By Request","Subscription Plan","Flex Plan","Everyday Plan", "Welocme")
self.pageImages = NSArray(objects: "page1","page2","page3","page4","page5")
self.pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "contentViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let startVC = self.viewControllerAtIndex(index: 0) as ContentViewController
let viewControllers = NSArray(object: startVC)
self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .forward, animated: true, completion: nil)
self.pageViewController.view.frame = CGRect(x: 0, y:0, width: Int(self.view.frame.width), height: Int(self.view.frame.size.height - 60))
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMove(toParentViewController: self)
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func loginAction(_ sender: AnyObject) {
}
@IBAction func signupAction(_ sender: AnyObject) {
}
func viewControllerAtIndex(index: Int) -> ContentViewController {
if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
return ContentViewController()
}
var vc: ContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "pageViewController") as! ContentViewController
vc.imageFile = self.pageImages[index] as! String
vc.titleText = self.pageTitles[index] as! String
vc.pageIndex = index
return vc
}
// MARK: - Page Vuew Controller Data Source
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if ((index == 0) || (index == NSNotFound)) {
return nil
}
index -= 1
return self.viewControllerAtIndex(index: index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var vc = viewController as! ContentViewController
var index = vc.pageIndex as Int
if (index == NSNotFound) {
return nil
}
index += 1
if (index == self.pageTitles.count) {
return nil
}
return self.viewControllerAtIndex(index: index)
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return self.pageTitles.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
}