ios - Swift 3 - UIPageViewController 实现返回空(空白) View

标签 ios swift xcode uipageviewcontroller

在我看来,这似乎是一个基本问题,但我已经搜索了好几天,但未能找到答案。

我有一个空的 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/

相关文章:

swift - 如何将 UInt8 转换为可以连接到字符串的数据类型?

ios - 如何轻松删除 Realm 中的所有对象

ios - 如何管理本地 Xcode 项目的传递依赖

ios - “GooglePlus/GooglePlus.h file not found” 尝试构建我的项目时

ios - Firestorage 关闭时显示图像的正确方法或在 Firestorage for iOS 中加载预览时出错

ios - 在 swift 中使用数组元素作为 var UILabel 名称

swift - 根据设备更改字体大小

ios - 为特定目标关联图像文件夹

使用 FaSTLane 翻译插件进行 iOS 本地化

ios - SLComposeViewController 在 ios8 中崩溃