在我看来,这似乎是一个基本问题,但我已经搜索了几天,并且找不到答案。
我有一个空的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
}
答案 0 :(得分:0)
以后引用具有相同问题的人。经过艰苦的调查,我看到的不是快速(至少3个)我必须在我打电话之前宣布故事板!可能是业余的错误。
所以之前:
let cVC: ContentViewController = self.storyboard?.instantiateViewController(withIdentifier: "ContentViewController") as! ContentViewController
我必须包括:
let storyboard = UIStoryboard(name: "Main", bundle: nil)