使用segue按下按钮后禁用PageViewController - Swift 4

Posted

技术标签:

【中文标题】使用segue按下按钮后禁用PageViewController - Swift 4【英文标题】:PageViewController Disabled after pressing button with segue - Swift 4 【发布时间】:2018-10-08 02:02:33 【问题描述】:

我成功实现了一个 PageViewController,我可以在其中滑动五个页面(ViewControllers)。问题是当我添加一个连接到其中一个 ViewController 的按钮时。它显示了页面,就像它应该的那样,但是我被困在那个页面上。 PageViewController 不再起作用。我不能再在页面之间滑动。

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate 

    lazy var subViewControllers:[UIViewController] = 
        return [
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController1") as! ViewController1,
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2") as! ViewController2,
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController3") as! ViewController3,
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController4") as! ViewController4,
            UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController5") as! ViewController5,
        ]
    ()

    override func viewDidLoad() 
    super.viewDidLoad()

        self.delegate = self
        self.dataSource = self

        setViewControllers([subViewControllers[0]], direction: .forward, animated: true, completion: nil)
    

// MARK - UIPageViewControllerDataSource

    func presentationCount(for pageViewController: UIPageViewController) -> Int 
        return subViewControllers.count
    

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? 
        let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
        if(currentIndex <= 0) 
            return nil
        
        return subViewControllers[currentIndex-1]
    

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? 
        let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
        if(currentIndex >= subViewControllers.count-1) 
            return nil
        
        return subViewControllers[currentIndex+1]
   

【问题讨论】:

不要将按钮与 segue 关联,而是将其与 IBAction 关联并在 IBAction 中调用 setViewControllers 我试过了,但我无法从按钮所在的 ViewController 访问 setViewControllers。上面的代码来自我的 PageViewController。该按钮位于 ViewController4 中,按下时显示 ViewController3。也许有一个我可以创建的功能。有什么代码可以推荐吗? 【参考方案1】:

您可以将PageViewController 的引用传递给您的子视图控制器,以便您可以从子视图控制器调用PageViewController 的方法。例如,创建

var pageViewController : PageViewController?

ViewController4 然后在subViewControllers 中赋值

lazy var subViewControllers:[UIViewController] = 
    let viewController4 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController4") as! ViewController4
    viewController4.pageViewController = self
    return [
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController1") as! ViewController1,
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2") as! ViewController2,
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController3") as! ViewController3,
        viewController4,
        UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController5") as! ViewController5,
    ]
()

然后您可以从ViewController4 调用PageViewController 的任何方法。为了简单起见,你可以在PageViewController中创建一个scrollToViewController3()方法

func scrollToViewController3()

    setViewControllers([subViewControllers[2]], direction: .reverse, animated: true, completion: nil)

并在ViewController4中致电pageViewController?.scrollToViewController3()

【讨论】:

以上是关于使用segue按下按钮后禁用PageViewController - Swift 4的主要内容,如果未能解决你的问题,请参考以下文章

在swift3中按下按钮时如何以编程方式执行segue或呈现视图

使用按下按钮和数组行的标识来执行 segue

按下按钮并成功重定向到另一个页面后,如何禁用导航栏链接?

两个不同的按钮对同一个视图控制器执行 segue,但希望通过一个按钮禁用用户交互以进行 segue

如何在按下按钮后禁用按钮,然后重新启用它?

单击不是提交按钮后如何禁用html按钮