在我看来,这似乎是一个基本问题,但我已经搜索了好几天,但未能找到答案。
我有一个空的 View Controller ,我试图在其中显示一个 UIPageViewController,其中填充了来自另一个 VC 的内容。我尝试了很多方法来做到这一点,但没有成功。我正在关注本教程:https://www.veasoftware.com/posts/uipageviewcontroller-in-swift-xcode-62-ios-82-tutorial ,我尝试按照它所解释的几乎完全相同的方式进行所有操作。有人能指出可能出了什么问题吗?
顺便说一句,我的类的 StoryboardID 设置正确,并且每个 VC 都连接到其相应的类。
这是我的初始 VC 代码:
override func viewDidLoad() {
super.viewDidLoad()
pageViewController = self.storyboard?.instantiateViewController(withIdentifier: "PageViewController") as? UIPageViewController
pageViewController!.dataSource = self
let startVC = self.viewControllerAt(index: 0)
let viewControllers = NSArray(object: startVC)
pageViewController!.setViewControllers(viewControllers as! [ContentViewController], direction: .forward, animated: false, completion: nil)
self.pageViewController!.view.frame = self.view.bounds
addChildViewController(pageViewController!)
self.view.addSubview((pageViewController!.view)!)
pageViewController?.didMove(toParentViewController: self)
}
let cVC: ContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
cVC.title = "Title \(index)"
cVC.nAuthor = "Author"
cVC.nCategory = "Sports"
cVC.nBody = "Lorem ipsum...."
cVC.pageIndex = index
return cVC
}
这是我的内容类:
override func viewDidLoad() {
super.viewDidLoad()
newsTitle.text = nTitle
newsAuthor.text = nAuthor
newsCategory.text = nCategory
newsBody.text = nBody
}
最佳答案
供以后遇到同样问题的人引用。经过艰苦的调查,我发现在 swift (至少 3)中我必须在调用它之前声明 Storyboard!可能是业余错误。
所以之前:
let cVC: ContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
我必须包括:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
关于ios - Swift 3 - UIPageViewController 实现返回空(空白) View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39981257/