我试图在我的iOS应用程序中制作一个教程屏幕,用户通过几张照片滑动了解有关该应用程序的一些信息。一切都看起来不错,除非我构建我的项目,我在第一行代码中收到以下错误:
Class ViewController has no initializers
我做错了什么?
我的ViewController.swift代码如下
import UIKit //line below is where I get the error class ViewController: UIViewController,UIPageViewControllerDataSource { //end var pageViewController: UIPageViewController var pageTitles: NSArray! var pageImages: NSArray! override func viewDidLoad() { super.viewDidLoad() self.pageTitles = NSArray(objects: "","","") self.pageImages = NSArray(objects: "page1","page2","page3") self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController self.pageViewController.dataSource = self var startVC = self.viewControllerAtIndex(0) as ContentViewController var viewControllers = NSArray(object: startVC) self.pageViewController.setViewControllers(viewControllers as [AnyObject],direction: .Forward,animated: true,completion: nil) self.addChildViewController(self.pageViewController) self.view.addSubview(self.pageViewController.view) self.pageViewController.didMoveToParentViewController(self) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func restartAction(sender: AnyObject) { } func viewControllerAtIndex(index: Int) -> ContentViewController { if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) { return ContentViewController() } var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController vc.imageFile = self.pageImages[index] as! String vc.titleText = self.pageTitles[index] as! String vc.pageIndex = index return vc } // Mark: - Page View Controller Data Source func pageViewController(pageViewController: UIPageViewController,viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? { var vc = viewController as! ContentViewController var index = vc.pageIndex as Int if (index == 0 || index == NSNotFound) { return nil } index-- return self.viewControllerAtIndex(index) } func pageViewController(pageViewController: UIPageViewController,viewControllerAfterViewController viewController: UIViewController) -> UIViewController? { var vc = viewController as! ContentViewController var index = vc.pageIndex as Int if (index == NSNotFound) { return nil } index++ if (index == self.pageTitles.count) { return nil } return self.viewControllerAtIndex(index) } func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int { return self.pageTitles.count } func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int { return 0 } }
这是因为这一行:
原文链接:https://www.f2er.com/swift/320361.htmlvar pageViewController: UIPageViewController
你说的是,pageViewController不能为null,但是你没有一个初始化器将它设置为一个值。看你的代码,我怀疑你想把它改为:
var pageViewController: UIPageViewController!