IOS - 在展开Optional值

时间:2015-12-07 11:43:00

标签: ios uiimageview uistoryboard uipageviewcontroller

您好我正在IOS上实现教程屏幕,所以我使用的是PageViewController。我正在学习本教程

https://www.youtube.com/watch?v=OnC8XVdHsVM

教程代码可以在这里找到

https://github.com/simplyi/UIPageVIewControllerExample

当我运行应用程序时出现此错误

"在展开“可选”值时出乎意料地发现nil

该计划正在此处停止

TutorialScreenViewController.swift

var imageFileName: String!
 override func viewDidLoad() {
    super.viewDidLoad()


  myImageView.image = UIImage(named:imageFileName)
   }

在另一个控制器名称 ViewController.swift 我这样做

    override func viewDidLoad() {
            super.viewDidLoad()

            pageImages = NSArray(objects: "startscreen","startscreen2","startscreen3")


            self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("MyPageViewController") as! UIPageViewController

            self.pageViewController.dataSource = self

            let initialContenViewController = self.pageTutorialAtIndex(0) as TutorialScreenViewController

            let viewControllers = NSArray(object: initialContenViewController)

            self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
           /* self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)*/

            self.pageViewController.view.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height-100)

            self.addChildViewController(self.pageViewController)
            self.view.addSubview(self.pageViewController.view)
            self.pageViewController.didMoveToParentViewController(self)

        }
  func pageTutorialAtIndex(index: Int) -> TutorialScreenViewController
    {

        let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("TutorialScreenViewController") as! TutorialScreenViewController

        pageContentViewController.imageFileName = pageImages[index] as! String
        pageContentViewController.pageIndex = index


        return pageContentViewController

    }

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

        let viewController = viewController as! TutorialScreenViewController
        var index = viewController.pageIndex as Int

        if(index == 0 || index == NSNotFound)
        {
            return nil
        }

        index--

        return self.pageTutorialAtIndex(index)

    }

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

     {
        let viewController = viewController as! TutorialScreenViewController
        var index = viewController.pageIndex as Int

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

        index++

        if(index == pageImages.count)
        {
            return nil
        }

        return self.pageTutorialAtIndex(index)
    }



    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int

    {

    return pageImages.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int // The selected item reflected in the page indicator.

    {
    return 0

    }

任何人都知道这条线有什么问题。

1 个答案:

答案 0 :(得分:0)

在viewDidLoad中你必须设置var imageFileName:String!在使用它之前

创建一个类似于(var name:type!)的var意味着您创建了一个类型类型的可选项,并将其设置为nil但是可以访问它而无需打开它,因为!并不是 ? ,你永远不会设置imageFileName !!

您需要做的是在使用之前在viewDidLoad中设置imageFileName:

 override func viewDidLoad() {
super.viewDidLoad()
if imageFileName == nil {
imageFileName = "startscreen"
}
myImageView.image = UIImage(named:imageFileName)
}