在Swift中的PageViewController,索引将评估为nil

时间:2015-04-27 18:04:48

标签: ios swift uipageviewcontroller

我参加了一个最受欢迎的在线教程(http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/),关于使用Storyboard制作UIPageViewController并尝试将其转换为Swift。

正如人们所料,我遇到了一些问题。

这是我的代码:

PageContentViewController.swift

import UIKit

class PageContentViewController: UIViewController {

@IBOutlet weak var backgroundImageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
var pageIndex:Int!
var titleText:String!
var imageFile:String!


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.backgroundImageView.image = UIImage(named: self.imageFile)
    self.titleLabel.text = self.titleText

}

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

ViewController.swift

 import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource {

@IBAction func startWalkthrough(sender: AnyObject) {
}
var pageViewController:UIPageViewController!
var pageTitles:Array<String>!
var pageImages:Array<String>!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.pageTitles = ["Over 200 Tips and Tricks", "Discover Hidden Features", "Bookmark Favorite Tip", "Free Regular Update"]
    self.pageImages = ["page1.png", "page2.png", "page3.png", "page4.png"]



    // Create page view controller

    self.pageViewController = self.storyboard!.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
    self.pageViewController.dataSource = self
    var startingViewController:PageContentViewController  = self.viewControllerAtIndex(0)!
    var viewControllers:Array<PageContentViewController> = [startingViewController]
    self.pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)

    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30)
    self.addChildViewController(pageViewController)
    self.view.addSubview(pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)


}

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


func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return self.pageTitles.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
}


//helper method to get the index of current page

func viewControllerAtIndex(index: Int)-> PageContentViewController?{

    if((self.pageTitles.count == 0) || (index >= self.pageTitles.count))
    {
        return nil
    }

    var pageContentViewController: PageContentViewController = self.storyboard!.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController
    pageContentViewController.imageFile = self.pageImages[index]
    pageContentViewController.titleText = self.pageTitles[index]
    pageContentViewController.pageIndex = index

    return pageContentViewController

}


func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?{

    var index: Int? = PageContentViewController().pageIndex!
    if ((index! == 0) || index! == NSNotFound)
    {
        return nil
    }
    else {
        index! = index! - 1
        println(index!)
        return viewControllerAtIndex(index!)

    }
}


func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?{
    var index:Int? = PageContentViewController().pageIndex!

    println(index!)
    if (index! == NSNotFound)
        {
            return nil
        }

    index! = index! + 1
    if(index! == self.pageTitles.count)
        {
        return nil
        }
    println(index!)
    return viewControllerAtIndex(index!)
}


}

以下是问题:

1)当我运行应用程序时,它会加载第一张图片,但向右滑动会导致运行时错误,这表明页面索引等于nil。然后我尝试在PageContentViewController

中初始化索引
var pageIndex:Int! = 0

但这不起作用。索引不再是零,但是左右滑动的整个逻辑都是下降的。它仅从0到1进行计数,然后返回0而不是递增到2。

2)第二件事是我的标签和按钮不可见,看起来它们被图片覆盖,不知道为什么

如果有人对这个指数问题有所了解,我将非常感激

提前致谢!

1 个答案:

答案 0 :(得分:2)

您没有在委托方法中使用局部变量PageContentViewController().pageIndex!,而是使用静态pageIndex。这就是func viewControllerAtIndex(index: Int) -> FeaturedRecipeViewController { let featuredRecipeViewController = FeaturedRecipeViewController(nibName: "FeaturedRecipeViewController", bundle: nil) featuredRecipeViewController.pageIndex = index return featuredRecipeViewController } 始终相同的原因。

以下是我实施的方法,因此您可以做类似的事情。

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    let index = (viewController as! FeaturedRecipeViewController).pageIndex // This is the line you are looking for

    if index == pages.count - 1 {
        index = 0
    } else {
        index++
    }
    return viewControllerAtIndex(index)
}

请注意,我正在为ViewController对象设置索引。

当我在寻找下一个ViewController时,我会这样做:

{{1}}

我改变了代码,因为它在我的情况下做了一些非常奇怪的事情,但这应该与你正在寻找的东西类似。