Swift:如何以编程方式推进容器视图内的 UIPageViewController? [英] Swift: How to programatically advance a UIPageViewController that is inside a Container View?

查看:24
本文介绍了Swift:如何以编程方式推进容器视图内的 UIPageViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ViewController,其中包含一个按钮和两个我想永久保留的图像视图,还有一个 PageViewController 可以通过其他三个 ViewController 滑动,这一切都很好,但是我不知道该怎么做是如何在按下按钮时以编程方式转到下一张幻灯片.

I have one ViewController that contains a button and two image views that I would like to stay permanently, and a PageViewController that swipes through three other ViewControllers, and this all works well, however what I can't figure out how to do is how to go to the next slide programatically when the button is pressed.

这是我的 PageController 类的样子(基于 Fattie 在此处提供的非常有用的答案:UIPageViewController 和故事板):

Here's what my PageController class looks like (based on Fattie's very helpful answer here: UIPageViewController and storyboard):

class PageController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {


var pages = [UIViewController]()

override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
    self.dataSource = self

    let p1: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "onboardingText01")
    let p2: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "onboardingText02")
    let p3: UIViewController! = storyboard?.instantiateViewController(withIdentifier: "onboardingText03")

    pages.append(p1)
    pages.append(p2)
    pages.append(p3)

    setViewControllers([p1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)

}


func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController)-> UIViewController? {

    let currentPage = pages.index(of: viewController)!

    // used to prevent the PageViewController from looping
    if currentPage == 0 { return nil }

    let prev = abs((currentPage - 1) % pages.count)
    return pages[prev]

}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController)-> UIViewController? {

    let currentPage = pages.index(of: viewController)!

    // used to prevent the PageViewController from looping
    if currentPage == (pages.count - 1) { return nil }

    let nxt = abs((currentPage + 1) % pages.count)
    return pages[nxt]
}

func presentationIndex(for pageViewController: UIPageViewController)-> Int {
    return pages.count
}

}

然后将按钮定义为包含所有这些(单独文件)的 ViewController 中的操作.

The button is then defined as an action in the ViewController holding all these (separate file).

我尝试使用通知/观察器,但出现错误

I tried using a notification/observer but I get error

类型 'UIPageViewController' 没有成员 'advancePage'

Type 'UIPageViewController' has no member 'advancePage'

使用选择器(我不确定如何调用该函数).

with the selector (I'm not sure how to call the function).

内部 viewDidLoad:

Inside viewDidLoad:

NotificationCenter.default.addObserver(self, selector: #selector(UIPageViewController.advancePage()), name: advance, object: nil)

UIPageViewController 类内部

Inside the UIPageViewController Class

    func advancePage(){
        setViewControllers(pages,
                           direction: UIPageViewController.NavigationDirection.forward,
                           animated: true,
                           completion: nil)
    }

有什么想法吗?

推荐答案

你需要在 PageControllerviewDidLoad

NotificationCenter.default.addObserver(self, selector: #selector(self.advancePage), name: advance, object: nil)

<小时>

@objc func advancePage(_ note:NSNotification){

这篇关于Swift:如何以编程方式推进容器视图内的 UIPageViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆