在Swift中使用UIPageViewController并使用“as”向下转换

时间:2015-12-25 16:33:12

标签: swift uipageviewcontroller

我正在使用PageViewController,然后我为PageViewController声明了数据源,并且当实现所需的方法时,我在使用as downcast时出现错误,它说“correctViewController不是类型。”

import UIKit

class HarishPageViewController: UIPageViewController, UIPageViewControllerDataSource {

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    // Do any additional setup after loading the view.
}

let pageTitles = ["HarishIntroduction", "HarishPlaces", "WyomingSeminary", "AppleTree", "KearneyHighSchool", "MosconeCenter"]
let viewControllerArray: NSArray = [HarishIntroduction(), HarishPlaces(), WyomingSeminary(), AppleTree(), KearneyHighSchool(), MosconeCenter()]
var index = 0

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    if index == 0 {
        return nil
    }
    else {
        index--
        let page = pageTitles[index]
        let correctViewController: UIViewController = viewControllerArray[index] as! UIViewController
        let vc = harishStoryboard.instantiateViewControllerWithIdentifier(page) as correctViewController

    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Ignore the error next to the class that should disappear once I include all of the required methods.

我尝试了很多不同的方法,但我只是无法摆脱as旁边的错误。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

它不是类型或类。您正在尝试向下转换为变量。

as之后只需编写控制器的类