gpt4 book ai didi

swift - 使用按钮更改 PageViewController 会使最后一页上的应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 10:43:54 27 4
gpt4 key购买 nike

我正在使用按钮来更改页面控制,它发生了变化,但是当它到达最后一页时,问题就出现了,应用程序崩溃了。这是我更改页面的代码

func backBtnClicked() {
if currentIndex > 0 {
currentIndex -= 1
}
if currentIndex < 1 {
backBtn.isHidden = true
}
if currentIndex >= 0 {
nextBtn.setTitle("Next", for: .normal)
let startingViewController: TableVC? = viewControllerAtIndex(index: currentIndex)
let viewControllers = [startingViewController]
pageController?.setViewControllers(viewControllers as? [UIViewController], direction: .reverse, animated: false)
}

}

@objc func nextBtnClicked() {
if currentIndex != NSNotFound {
currentIndex += 1
}

if currentIndex == pg.count - 1 {
//Navigate Outside Pageview controller
log("lastt")
} else {


let startingViewController: TableVC? = viewControllerAtIndex(index: currentIndex)
let viewControllers = [startingViewController]
pageController?.setViewControllers(viewControllers as? [UIViewController], direction: .forward, animated: false)
}

}

这是我遇到的错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'The number of view controllers provided (0) doesn't match the number required (1) for the requested transition'

任何帮助

最佳答案

替换

pageController?.setViewControllers(viewControllers as? [UIViewController], direction: .forward, animated: false)

pageController?.setViewControllers(viewControllers, direction: .forward, animated: false)

成为

let startingViewController = viewControllerAtIndex(index: currentIndex)!
pageController?.setViewControllers([startingViewController], direction: .forward, animated: false)
<小时/>

按后退

guard currentIndex - 1 >= 0 else { return }
currentIndex -= 1
// get the vc

下一次按下

guard currentIndex + 1 < pg.count else { return }
currentIndex += 1
// get the vc

在您当前的代码中,似乎这个 let runningViewController: TableVC? 在某个时刻返回 nil,这使得这个 viewControllers as? [UIViewController] 为空数组

关于swift - 使用按钮更改 PageViewController 会使最后一页上的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56209469/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com