Nicolas Rudisky Nicolas Rudisky - 6 months ago 70
Swift Question

UIPageViewController in Swift

Hi am following a tutorial on UIPageViewController to generate a gallery of pictures in my project http://www.theappguruz.com/blog/easy-steps-to-implement-uipageviewcontroller-in-swift even download the demo and it works but in my if I get an error and the application falls.


fatal error: unexpectedly found nil while unwrapping an Optional value


enter image description here

at next my code.

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
}



}

Answer

I believe Joakin is right, you are forcing the unwrap of an optional value, you need to either initialize your values when you declare them or treat them as optionals and unwrap them as you need them.

Try this:

var strTitle: String?
var strPhotoName: String?

override func viewDidLoad() {
    super.viewDidLoad()
    if let photoName = strPhotoName {
     imageView.image = UIImage(named: photoName)
    }

    if let title = strTitle {
     lblTitle.text = title
    }
}

Then, somewhere in your code you need to specify the name of the image (make sure the image exists in your project) and the name of the title. If you dont specify them, the updated lines of code will not get executed.

Read more about optional values here

Comments