如何访问UIPageViewController的最后一项,以便可以将self设置为其委托?
self.present(wizard, animated: true, completion: {
let lastVC = wizard.viewControllers?.last as! thirdPageController
lastVC.delegate = self
})
我认为第三项尚未加载,因此.last
返回了错误的页面(第一项)
答案 0 :(得分:2)
快速5
class MyPageViewController: UIPageViewController {
private lazy var _viewControllers = [UIViewController]()
public var lastViewController: UIViewController? {
return _viewControllers.last
}
override func setViewControllers(_ viewControllers: [UIViewController]?, direction: UIPageViewController.NavigationDirection, animated: Bool, completion: ((Bool) -> Void)? = nil) {
super.setViewControllers([viewController], direction: direction, animated: animated, completion: completion)
_viewControllers.removeAll()
_viewControllers.append(viewControllers)
}
}
课程用法:
let myPageViewController = MyPageViewController()
myPageViewController.lastViewController // This is an optional value
最后,您应该将 wizard
UIPageViewController
的类别更改为 MyPageViewController
然后,您可以使用以下新类:
self.present(wizard, animated: true, completion: {
if let lastViewController = wizard.lastViewController as? thirdPageController {
lastViewController.delegate = self
}
})