【问题标题】:How Do I Add UIViewController to UIScrollView in Swift如何在 Swift 中将 UIViewController 添加到 UIScrollView
【发布时间】:2015-04-15 12:50:15
【问题描述】:

我想要一个 UIViewControllers 的 UIScrollView。我测试了 ScrollView,它工作正常,除非我尝试添加 ViewControllers。该项目构建良好,但我收到指向 ViewController 的“fatal error: unexpectedly found nil while unwrapping an Optional value”错误,尤其是以下行:

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

但是,当我检查对象时,我可以看到变量 imagetext 在 ViewController 中有值。我正在使用 Xcode 6.3 Beta 并构建到 iOS8.1 目标。

代码如下:

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.
}}

这是我要添加的 ViewController 的代码:

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.

    }}

【问题讨论】:

  • 也许 UIImage() 返回 nil

标签: ios iphone swift uiviewcontroller uiscrollview


【解决方案1】:

修复方法是从情节提要创建PostViewControllerinstantiateViewControllerWithIdentifier,这样,当它的视图添加到滚动视图时,它将从情节提要加载一个 imageView。

【讨论】:

  • 感谢您的快速响应。更改不起作用...添加 PostViewController 作为父视图的子视图 self.addChildViewController(vc) 仍然收到错误...
  • 请使用 Storyboard ID 从 Storyboard 创建 PostViewController,您似乎没有 imageView。
  • 好的。问题是,我为PostViewController 创建了一个单独的 XIB。是否有与 instantiateViewControllerWithIdentifier 等效的方法可以用来从我创建的 XIB 中实例化?
  • 你可以使用initWithNibName
  • 使用iniitWithNibName 有效。谢谢!我假设我需要单独添加要放置的每个 ViewController,因为我创建的 VC 数组没有显示出来。对吗?
猜你喜欢
  • 2017-11-21
  • 2023-03-28
  • 1970-01-01
  • 2014-08-18
  • 2019-06-05
  • 2017-01-04
  • 2011-01-07
  • 1970-01-01
相关资源
最近更新 更多