UIPageViewController in Swift

0

Hi, I'm following a tutorial on UIPageViewController to generate a gallery of images in my project link even download the demo and it works but in my case it gives me an error and the application is dropped.

  

fatal error: unexpectedly found nil while unwrapping an Optional value

I attach the code of my controllers.

PageContentViewController.swift:

import UIKit

class PageContentViewController: UIPageViewController {


    @IBOutlet weak var lblTitle: UILabel!

    @IBOutlet weak var imageView: UIImageView!


    var pageIndex: Int = 0
    var strTitle: String!
    var strPhotoName: String!


    override func viewDidLoad() {
        super.viewDidLoad()

        imageView.image = UIImage(named: strPhotoName)
        lblTitle.text = strTitle

    }
}

ViewController2.swift

import UIKit

class ViewController2: UIPageViewController, UIPageViewControllerDataSource
{
    var arrPageTitle: NSArray = NSArray()
    var arrPagePhoto: NSArray = NSArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        arrPageTitle = ["This is The App Guruz", "This is Table Tennis 3D", "This is Hide Secrets"];
        arrPagePhoto = ["1.jpg", "2.jpg", "3.jpg"];

        self.dataSource = self

        self.setViewControllers([getViewControllerAtIndex(0)] as [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    }

    // MARK:- UIPageViewControllerDataSource Methods

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
    {
        let pageContent: PageContentViewController = viewController as! PageContentViewController

        var index = pageContent.pageIndex

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

        index -= 1;
        return getViewControllerAtIndex(index)
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
    {
        let pageContent: PageContentViewController = viewController as! PageContentViewController

        var index = pageContent.pageIndex

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

        index += 1;
        if (index == arrPageTitle.count)
        {
            return nil;
        }
        return getViewControllerAtIndex(index)
    }

    // MARK:- Other Methods
    func getViewControllerAtIndex(index: NSInteger) -> PageContentViewController
    {
        // Create a new view controller and pass suitable data.
        let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController

        pageContentViewController.strTitle = "\(arrPageTitle[index])"
        pageContentViewController.strPhotoName = "\(arrPagePhoto[index])"
        pageContentViewController.pageIndex = index

        return pageContentViewController
    }



}
    
asked by Nicolas Rudisky 30.05.2016 в 19:02
source

1 answer

0

The error is either in the outlet connection of imageView that is not correct or in which strPhotoName is empty (is nil ) and the image is not loaded correctly.

Update 1 After reviewing the code and as I said, the problem is in UIImageView . When instantiating the PageContentViewController property imageView == nil . You have to check the connections and the storyboard to see what is going wrong. In the following image you can see the problem with a simple breakpoint :

Update 2

In this link can be see a possible solution to the problem.

    
answered by 30.05.2016 в 20:34