Swift 为什么将my UIPageViewController的转换样式更改为.scroll会使所有UIViewController消失?
我正在尝试将UIPageViewController的转换样式从默认的pageCurl更改为scroll。我试着用程序来完成这一切。在下面显示的代码中,当注释掉这两个init函数时,我的两个视图控制器,Swift 为什么将my UIPageViewController的转换样式更改为.scroll会使所有UIViewController消失?,swift,Swift,我正在尝试将UIPageViewController的转换样式从默认的pageCurl更改为scroll。我试着用程序来完成这一切。在下面显示的代码中,当注释掉这两个init函数时,我的两个视图控制器,teamVC和teamVC2显示得非常好 override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.Navigatio
teamVC
和teamVC2
显示得非常好
override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
然而,当他们留在我运行的代码,没有出现在屏幕上
class PagingViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var teamViewControllers: [UIViewController] = []
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex = teamViewControllers.firstIndex(of: viewController) ?? 0
if currentIndex <= 0 {
return nil
}
return teamViewControllers[currentIndex - 1]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex = teamViewControllers.firstIndex(of: viewController) ?? 0
if currentIndex >= teamViewControllers.count - 1 {
return nil
}
return teamViewControllers[currentIndex + 1]
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return teamViewControllers.count
}
//this function and the required init function below make my view controllers disappear
override init(transitionStyle style: UIPageViewController.TransitionStyle, navigationOrientation: UIPageViewController.NavigationOrientation, options: [UIPageViewController.OptionsKey : Any]? = nil) {
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let teamVC = TeamViewController()
teamVC.teamName = "Atlanta Hawks"
teamViewControllers.append(teamVC)
let teamVC2 = TeamViewController()
teamVC2.teamName = "Boston Celtics"
teamViewControllers.append(teamVC2)
setViewControllers([teamViewControllers[0]], direction: .forward, animated: true, completion: nil)
}
}
class PagingViewController:UIPageViewController、UIPageViewControllerDataSource、UIPageViewControllerDelegate{
var TeamViewController:[UIViewController]=[]
func pageViewController(uPageViewController:UIPageViewController,viewController在viewController:UIViewController之前)->UIViewController{
让currentIndex=TeamViewController.firstIndex(of:viewController)??0
如果是currentIndex UIViewController{
让currentIndex=TeamViewController.firstIndex(of:viewController)??0
如果currentIndex>=TeamViewController.count-1{
归零
}
返回TeamViewController[currentIndex+1]
}
func presentationCount(对于pageViewController:UIPageViewController)->Int{
返回teamViewControllers.count
}
//此函数和下面所需的init函数使my view控制器消失
重写初始化(transitionStyle:UIPageViewController.transitionStyle,navigationOrientation:UIPageViewController.navigationOrientation,选项:[UIPageViewController.OptionKey:Any]?=nil){
super.init(transitionStyle:.滚动,navigationOrientation:.水平,选项:nil)
}
必需初始化?(编码器:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
重写func viewDidLoad(){
super.viewDidLoad()
self.delegate=self
self.dataSource=self
让teamVC=TeamViewController()
teamVC.teamName=“亚特兰大鹰队”
TeamViewController.append(teamVC)
让teamVC2=TeamViewController()
teamVC2.teamName=“波士顿凯尔特人”
TeamViewController.append(teamVC2)
SetViewController([TeamViewController[0]],方向:。正向,动画:true,完成:nil)
}
}
我看不到任何创建PagingViewController并将其放在屏幕上的代码。它在哪里?为什么要重写init
?你应该只调用init
,而不是重写它。另外,让页面视图控制器成为它自己的委托是一个非常糟糕的主意。一般来说,这不是视图控件oller说,您应该完全需要子类化。