setViewControllers不起作用

问题描述 投票:3回答:1

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

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

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

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
}

这是连接到我的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()
}

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

我将衷心感谢您的帮助

我一直在努力,因为这已经有一段时间了

非常感谢你 :)

编辑:也许这发生是因为UIPageViewController在containerView中。但我不确定

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

ios swift xcode uipageviewcontroller uicontainerview
1个回答
2
投票

好的 - 问题是你的代码在viewDidLoad()

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

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

您需要做的是在prepare(for segue:...)中获取它的引用,并将其设置为您的代理:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.