1

I want to have a UIScrollView of UIViewControllers. I tested the ScrollView and it works fine, except when I try to add the ViewControllers. The project builds fine, but I get a "fatal error: unexpectedly found nil while unwrapping an Optional value" error that points at the ViewController, particularly the line:

self.imageView.image = UIImage(named: self.image!)

However, when I inspect the object, I can see variables image and text have values in the ViewController. I'm using Xcode 6.3 Beta and building to iOS8.1 target.

Here's the code:

import UIKit

class ViewController: UIViewController {

//@IBOutlet weak var contentScrollView: UIScrollView!

@IBOutlet var contentScrollView: UIScrollView!

//test to make sure ScrollView functions
//let colors = [UIColor.redColor(),UIColor.blueColor(), UIColor.greenColor(), UIColor.whiteColor(), UIColor.blackColor(), UIColor.yellowColor()]

var frame = CGRectMake(0, 0, 0, 0)

var dataArray = [PostItem]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var da = PostItem(text: "test 1", pic: "beans.jpg")
    self.dataArray.append(da!)

    da = PostItem(text: "test 2", pic: "coffee.jpg")
    self.dataArray.append(da!)

    da = PostItem(text: "test 3", pic: "coffeeCup.jpg")
    self.dataArray.append(da!)

    for index in 0..<self.dataArray.count{
        self.frame.origin.y = self.contentScrollView.frame.size.height * CGFloat(index)
        self.frame.size = self.contentScrollView.frame.size
        self.contentScrollView.pagingEnabled = false
        let tempPost = self.dataArray[index]


        var vc = PostViewController()
        vc.text = tempPost.postText
        vc.image = tempPost.postImage

        self.contentScrollView.addSubview(vc.view)

    }

    self.contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGFloat(self.dataArray.count) * self.contentScrollView.frame.height)
}

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

And here is the code for the ViewController I want to add:

import UIKit

class PostViewController: UIViewController {

    //let postItem
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var postToFBButton: UIButton!
    @IBOutlet weak var postToTwitterButton: UIButton!
    @IBOutlet weak var postText: UILabel!

    var image:String?
    var text:String?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.imageView.image = UIImage(named: self.image!)
        self.postText.text = text!
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }}
Tony Armstrong
  • 638
  • 7
  • 17

1 Answers1

0

The fix is to create the PostViewController through instantiateViewControllerWithIdentifier from storyboard, this way, it will have an imageView loaded from storyboard when its view is added to the scrollview.

gabbler
  • 13,626
  • 4
  • 32
  • 44
  • thanks for the quick response. The change didn't work... Added PostViewController as a child of the parent view as `self.addChildViewController(vc)` still getting the error... – Tony Armstrong Feb 14 '15 at 16:02
  • 2
    Please create the PostViewController from storyboard with a storyboard ID, it seems you don't have an imageView. – gabbler Feb 14 '15 at 16:10
  • OK. Problem is, I created a separate XIB for `PostViewController`. Is there an equivalent method to `instantiateViewControllerWithIdentifier` that I can use to instantiate from the XIB I created? – Tony Armstrong Feb 14 '15 at 17:01
  • 2
    You can use `initWithNibName`. – gabbler Feb 14 '15 at 17:03
  • using `iniitWithNibName` worked. Thanks! I assume I need to add each ViewController that I want to place, individually, as the array of VCs I created isn't being displayed. Is that correct? – Tony Armstrong Feb 14 '15 at 21:22
  • Yes, you should do addChildViewController, see http://stackoverflow.com/a/17332864/4016786, it gives you more control on the view that is added. – gabbler Feb 15 '15 at 01:49