不知道为什么我在Swift iOS应用程序代码中出现“类视图控制器没有初始化器”错误?

时间:2021-09-29 21:06:51

I was trying to make a tutorial screen in my iOS app where the user swipes past a few pictures to learn a bit about the said application. Everything looks fine, except when I build my project, I get the following error on the very first line of code:

我试图在我的iOS应用程序中制作一个教程屏幕,用户在上面浏览几张图片,了解一下这个应用程序。一切看起来都很好,除了当我构建项目时,我在第一行代码中有以下错误:

Class ViewController has no initializers

类ViewController没有初始化器

What did I do wrong?

我做错了什么?

My ViewController.swift code is below

我的ViewController。斯威夫特代码下面

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
}

}

2 个解决方案

#1


30  

It's because of this line:

因为这句话:

var pageViewController: UIPageViewController

You're saying that pageViewController cannot be null, but you don't have an initializer that sets it to a value. Looking at your code, I suspect you want to change it to this:

你说pageViewController不能为空,但是你没有将它设置为值的初始化器。看看你的代码,我猜你想把它改成这样:

var pageViewController: UIPageViewController!

I went into more detail on this problem here.

我在这里更详细地讨论了这个问题。

#2


7  

In a Swift class each non-optional declared variable must be initialized in one of the given init methods.

在Swift类中,必须在给定的init方法中初始化每个非可选声明变量。

In your case, it's this variable

在你的例子中,它是这个变量。

 var pageViewController: UIPageViewController

declare it as implicit unwrapped optional as the other two variables, because the variable is guaranteed to be set before it's used.

声明它为隐式未封装的可选变量,就像其他两个变量一样,因为在使用变量之前必须设置变量。

 var pageViewController: UIPageViewController!

#1


30  

It's because of this line:

因为这句话:

var pageViewController: UIPageViewController

You're saying that pageViewController cannot be null, but you don't have an initializer that sets it to a value. Looking at your code, I suspect you want to change it to this:

你说pageViewController不能为空,但是你没有将它设置为值的初始化器。看看你的代码,我猜你想把它改成这样:

var pageViewController: UIPageViewController!

I went into more detail on this problem here.

我在这里更详细地讨论了这个问题。

#2


7  

In a Swift class each non-optional declared variable must be initialized in one of the given init methods.

在Swift类中,必须在给定的init方法中初始化每个非可选声明变量。

In your case, it's this variable

在你的例子中,它是这个变量。

 var pageViewController: UIPageViewController

declare it as implicit unwrapped optional as the other two variables, because the variable is guaranteed to be set before it's used.

声明它为隐式未封装的可选变量,就像其他两个变量一样,因为在使用变量之前必须设置变量。

 var pageViewController: UIPageViewController!