[英]setViewControllers doesn't work
I'm trying to change the current ViewController of my UIPageViewController using a NEXT button. 我正在尝试使用NEXT按钮更改我的UIPageViewController的当前ViewController。 So I'm calling the function that's in my containterViewController in my mainViewController using a delegate. 所以我使用委托调用我的mainViewController中的containterViewController中的函数。 But it doesn't execute the setViewControllers line. 但它不执行setViewControllers行。
Here you can see my code: 在这里你可以看到我的代码:
This is the method that I call using my delegate: 这是我使用我的委托调用的方法:
func forwardPage() {
print("Start")
currentPage += 1
print(vcs[currentPage])
self.setViewControllers([vcs[currentPage]], direction: .forward, animated: true) { (true) in
print("Done")
}
}
Here's my delegate: 这是我的代表:
protocol WalkthroughViewControllerDelegate {
func forwardPage()
func currentIndex() -> Int
}
And here's the function that is connected to my NEXT button: 这是连接到我的NEXT按钮的功能:
@IBAction func nextButtonTapped(_ sender: Any) {
if let index = delegate?.currentIndex() {
print("Here... \(index)")
switch index {
case 0...1:
print("Forwarding...")
delegate?.forwardPage()
case 2:
dismiss(animated: true, completion: nil)
default: break
}
}
updateUI()
}
Everything but the "Done" gets printed 除了“完成”之外的所有内容都会被打印出来
I would really appreciate your help 我将衷心感谢您的帮助
I've been struggling because of this for quite some time now 我一直在努力,因为这已经有一段时间了
Thank you very much :) 非常感谢你 :)
EDIT: Maybe this happens because that UIPageViewController is inside a containerView. 编辑:也许这发生是因为UIPageViewController在containerView中。 But I'm not sure 但我不确定
SECOND EDIT: I've created a git-hub repository just for this issue. 第二次编辑:我已经为这个问题创建了一个git-hub存储库。 Here's the link: https://github.com/LennartPhil/App-Popup-Screen . 这是链接: https : //github.com/LennartPhil/App-Popup-Screen 。 I hope you can understand that I won't show you all of my files. 我希望你能理解我不会向你展示我的所有文件。
OK - the problem is that your code in viewDidLoad()
: 好的 - 问题是你的代码在viewDidLoad()
:
let storyboard = UIStoryboard(name: "ExtraViewControllers", bundle: nil)
let walkthroughPageVC = storyboard.instantiateViewController(withIdentifier: "WalkthroughPageVC") as! WalkthroughPageViewController
delegate = walkthroughPageVC
is creating a NEW instance of WalkthroughPageViewController
, which goes out of scope as soon as viewDidLoad()
exits. 正在创建一个WalkthroughPageViewController
的新实例,一旦viewDidLoad()
退出,它就会超出范围。 So it no longer exists. 所以它不再存在。
What you need to do instead is get a reference to it in prepare(for segue:...)
, and set it as your delegate there: 您需要做的是在prepare(for segue:...)
获取对它的引用prepare(for segue:...)
,并将其设置为您的委托:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? WalkthroughPageViewController {
self.delegate = vc
}
}
I forked your GitHub repo and added the files into a project, so you can see it run: https://github.com/DonMag/App-Popup-Screen 我分叉你的GitHub仓库并将文件添加到一个项目中,所以你可以看到它运行: https : //github.com/DonMag/App-Popup-Screen
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.