Tony Armstrong
Tony Armstrong

Reputation: 638

UILabel - unexpectedly found nil while unwrapping an Optional value

Creating a page dynamically using this code, which creates a PageViewController

import UIKit

class PageViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var pvc:UIPageViewController?
var testArray: [String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    pvc = UIPageViewController(
            transitionStyle: .Scroll,
            navigationOrientation: .Horizontal,
            options: nil )

    // Make this object the datasource and delegate for the PageViewController
    self.pvc?.delegate = self
    self.pvc?.dataSource = self

    // Populate an array of test content for the pages
    testArray = ["Page 1", "Page 2", "Page 3"]

    // Create an array of child pages
    var swipePage:SwipePageController = SwipePageController()
    println("Page view Controller: \(testArray[0])")
    swipePage.name = testArray[0]
    var pageArray: NSArray = [swipePage]

    self.pvc?.setViewControllers(pageArray, direction: .Forward, animated: true, completion: nil)

}

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

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    let index:Int! = (viewController as SwipePageController).index!

    if (index >= self.testArray.count){
        return nil
    }

    index + 1

    return viewControllerAtIndex(index)

}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    let index:Int! = (viewController as SwipePageController).index!

    if index == NSNotFound {
        return nil
    }

    index - 1

    return viewControllerAtIndex(index)

}

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

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    let page = self.pvc?.viewControllers[0] as SwipePageController
    let name = page.name
    return find(self.testArray, name!)!
}

func viewControllerAtIndex(index: Int) -> SwipePageController?
{
    if self.testArray.count == 0 || index >= self.testArray.count
    {
        return nil
    }

    // Create a new view controller and pass suitable data.
    let swipeView = SwipePageController()
    swipeView.name = self.testArray[index]
    swipeView.index = index

    return swipeView
}

The println() about a 1/3 of the way down the code, does display the value I expect to see. The code for the ViewController I create dynamically is here:

import UIKit

class SwipePageController: UIViewController {

// var to hold the var to display to the interface
var name:String?
var index:Int?
@IBOutlet weak var testLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    println(name!)
    if let passedName = name? {
        self.testLabel.text = passedName
    }
}

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


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

println(name!) does show the correct value as well. However, I cannot populate the label with the value. I get the following error:

"fatal error: unexpectedly found nil while unwrapping an Optional value"

I'm at my wit's end...I cannot figure out what's not working. Also, see the screen shot of my Xcode interface where the error occurs:

screen cap of Xcode interface

Can anyone help?

Upvotes: 1

Views: 3747

Answers (1)

Mike Taverne
Mike Taverne

Reputation: 9352

I think the problem is the way you are instantiating the SwipePageController.

Instead of this:

var swipePage:SwipePageController = SwipePageController()

Try this:

var swipePage: SwipePageController = UIStoryboard(name: "YourStoryboardName", bundle: nil).instantiateViewControllerWithIdentifier("YourViewControllerId")

Replace "YourStoryboardName" with the actual name of your storyboard file, and "YourViewControllerId" with the Storyboard Id of your Swipe Page Controller. If you haven't already given it a Storyboard Id, make sure to set this in the Identity inspector.

Upvotes: 1

Related Questions