在这里的好人的帮助下,我已经学会了如何将UIViewController
添加到UIScrollView
- 正确的方法。问题是,我需要添加多个视图控制器。我尝试创建一个UIViewControllers数组,然后使用for...in
循环将控制器添加为子视图控制器,然后将其视图推送到滚动视图。这不起作用。
现在,我只在滚动视图中看到一个ViewController,第一个...任何人都可以告诉我我做错了什么?
请参阅我viewDidLoad()
中的相关代码以及我设置用于创建相关对象的属性:
@IBOutlet var contentScrollView: UIScrollView!
var frame = CGRectMake(0, 0, 0, 0)
var dataArray = [PostViewController]()
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")
var vc = PostViewController(nibName:"PostViewController", bundle:nil)
vc.text = da!.postText
vc.image = da!.postImage
self.dataArray.append(vc)
da = PostItem(text: "test 2", pic: "coffeeCup.jpg")
vc = PostViewController(nibName:"PostViewController", bundle:nil)
vc.text = da!.postText
vc.image = da!.postImage
self.dataArray.append(vc)
da = PostItem(text: "test 2", pic: "coffeeMill.jpg")
vc = PostViewController(nibName:"PostViewController", bundle:nil)
vc.text = da!.postText
vc.image = da!.postImage
self.dataArray.append(vc)
//...
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 vc = self.dataArray[index]
self.addChildViewController(vc)
self.contentScrollView.addSubview(vc.view)
}
self.contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width, CGFloat(self.dataArray.count) * self.contentScrollView.frame.height)
}