简体   繁体   中英

How Do I Add UIViewController to UIScrollView in Swift

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.

    }}

解决方法是创建PostViewController通过instantiateViewControllerWithIdentifier从情节串连图板,这样一来,将有来自故事板加载一个ImageView的时,其视图添加到滚动视图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM