[英]How Do I Add UIViewController to UIScrollView in Swift
I want to have a UIScrollView of UIViewControllers. 我想要一个UIViewControllers的UIScrollView。 I tested the ScrollView and it works fine, except when I try to add the ViewControllers. 我对ScrollView进行了测试,并且工作正常,除非尝试添加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: 该项目构建良好,但是我遇到了“ fatal error: unexpectedly found nil while unwrapping an Optional value
”错误,该错误指向ViewController,尤其是以下行:
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. 但是,当我检查对象时,可以在ViewController中看到变量image
和text
具有值。 I'm using Xcode 6.3 Beta and building to iOS8.1 target. 我正在使用Xcode 6.3 Beta并构建到iOS8.1目标。
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: 这是我要添加的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.
}}
解决方法是创建PostViewController
通过instantiateViewControllerWithIdentifier
从情节串连图板,这样一来,将有来自故事板加载一个ImageView的时,其视图添加到滚动视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.