【问题标题】:setViewControllers doesn't worksetViewControllers 不起作用
【发布时间】:2018-10-04 23:54:12
【问题描述】:

我正在尝试使用 NEXT 按钮更改 UIPageViewController 的当前 ViewController。因此,我正在使用委托在 mainViewController 中调用我的 containerViewController 中的函数。但它不执行 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
}

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

@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。希望您能理解,我不会向您展示我的所有文件。

【问题讨论】:

  • “我正在调用容器视图控制器中的函数” ... forwardPage() 在您的 UIPageViewController 类中吗?换句话说,self 是否有 UIPageViewController 的实例?
  • 而且...我使用嵌入在 containerView 中的页面视图控制器进行了测试,所以这不是问题(除非您的 func 位于错误的位置)。
  • 是的,self 的类型是 UIPageViewController
  • 嗯...不知道为什么你的代码不起作用。你有没有通过它来检查你的变量?我有一个简单的示例项目,它使用嵌入式页面视图控制器,以及以编程方式更改页面的按钮......并且尾随完成块 does 被调用。您可以看一下,并将我的方法与您的方法进行比较,看看是否有什么跳出来:github.com/DonMag/EmbeddedPageView
  • 非常感谢您的帮助。不幸的是,我真的找不到任何可以帮助我的东西。但无论如何,我真的很感激 :)

标签: ios swift xcode uipageviewcontroller uicontainerview


【解决方案1】:

好的 - 问题是你的代码在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
    }
}

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

【讨论】:

  • 非常感谢!我现在非常高兴 :) 真是太棒了,谢谢你^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-08
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多