gpt4 book ai didi

ios - setViewControllers 不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 22:39:36 27 4
gpt4 key购买 nike

我正在尝试使用 NEXT 按钮更改 UIPageViewController 的当前 ViewController。所以我使用委托(delegate)在我的 mainViewController 中调用我的 containterViewController 中的函数。但它不执行 setViewControllers 行。

在这里你可以看到我的代码:

这是我使用委托(delegate)调用的方法:

func forwardPage() {
print("Start")
currentPage += 1
print(vcs[currentPage])
self.setViewControllers([vcs[currentPage]], direction: .forward, animated: true) { (true) in
print("Done")
}
}

这是我的代表:

protocol WalkthroughViewControllerDelegate {
func forwardPage()
func currentIndex() -> Int
}

这是连接到我的“下一步”按钮的功能:

@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()
}

除了“完成”之外的所有内容都被打印

非常感谢你的帮助

我已经为此苦苦挣扎了一段时间

非常感谢:)

编辑:可能发生这种情况是因为 UIPageViewController 在 containerView 中。但我不确定

第二次编辑:我专门为这个问题创建了一个 git-hub 存储库。这是链接:https://github.com/LennartPhil/App-Popup-Screen .我希望您能理解,我不会向您展示我的所有文件。

最佳答案

好的 - 问题是您在 viewDidLoad() 中的代码:

    let storyboard = UIStoryboard(name: "ExtraViewControllers", bundle: nil)
let walkthroughPageVC = storyboard.instantiateViewController(withIdentifier: "WalkthroughPageVC") as! WalkthroughPageViewController
delegate = walkthroughPageVC

正在创建 WalkthroughPageViewController 的新实例,一旦 viewDidLoad() 退出,它就会超出范围。所以它不再存在。

您需要做的是在 prepare(for segue:...) 中获取对它的引用,并在那里将其设置为您的委托(delegate):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? WalkthroughPageViewController {
self.delegate = vc
}
}

我 fork 你的 GitHub 存储库并将文件添加到一个项目中,所以你可以看到它运行:https://github.com/DonMag/App-Popup-Screen

关于ios - setViewControllers 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50006440/

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