无法连接页面视图控制器上的按钮并使用按钮滑动到下一个屏幕

Posted

技术标签:

【中文标题】无法连接页面视图控制器上的按钮并使用按钮滑动到下一个屏幕【英文标题】:Unable to connect buttons on page view controller and use button to swipe to next screen 【发布时间】:2018-11-11 08:17:35 【问题描述】:

目前我有简单的入职/演练屏幕,但您只能在它们之间滑动。我还想添加一个下一步和返回按钮。我在情节提要上添加了下一个按钮,并尝试将其作为 IBAction 连接到我的 PageViewController.swift 文件,但实际上并没有连接。我还尝试以编程方式创建一个按钮,并在点击时打印到控制台,但我不知道如何让它执行与滑动相同的操作?

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource 


    var pageControl = UIPageControl()

    // MARK: UIPageViewControllerDataSource


    lazy var orderedViewControllers: [UIViewController] = 
        return [self.newVc(viewController: "sbBlue"),
                self.newVc(viewController: "sbRed")]
    ()

    override func viewDidLoad() 
        super.viewDidLoad()
        self.dataSource = self
        self.delegate = self



        // This sets up the first view that will show up on our page control
        if let firstViewController = orderedViewControllers.first 
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
        

        configurePageControl()

        // Do any additional setup after loading the view.
    

    func configurePageControl() 
        // The total number of pages that are available is based on how many available colors we have.
        pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
        self.pageControl.numberOfPages = orderedViewControllers.count
        self.pageControl.currentPage = 0
        self.pageControl.tintColor = UIColor.white
        self.pageControl.pageIndicatorTintColor = UIColor.lightGray
        self.pageControl.currentPageIndicatorTintColor = UIColor.white
        self.view.addSubview(pageControl)



    

    func newVc(viewController: String) -> UIViewController 
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: viewController)
    


    // MARK: Delegate methords
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) 
        let pageContentViewController = pageViewController.viewControllers![0]
        self.pageControl.currentPage = orderedViewControllers.index(of: pageContentViewController)!
    

    // MARK: Data source functions.
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? 
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else 
            return nil
        

        let previousIndex = viewControllerIndex - 1

        // User is on the first view controller and swiped left to loop to
        // the last view controller.
        guard previousIndex >= 0 else 
            //return orderedViewControllers.last
            // Uncommment the line below, remove the line above if you don't want the page control to loop.
            return nil
        

        guard orderedViewControllers.count > previousIndex else 
            return nil
        

        return orderedViewControllers[previousIndex]
    

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? 
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else 
            return nil
        

        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = orderedViewControllers.count

        // User is on the last view controller and swiped right to loop to
        // the first view controller.
        guard orderedViewControllersCount != nextIndex else 
            //return orderedViewControllers.first
            // Uncommment the line below, remove the line above if you don't want the page control to loop.
            return nil
        

        guard orderedViewControllersCount > nextIndex else 
            return nil
        

        return orderedViewControllers[nextIndex]
    

这是使用代码创建的按钮:

   let nextButton = UIButton()

    private func setupView() 



        nextButton.frame = CGRect(x: self.view.frame.size.width - 60, y: 60, width: 50, height: 50)
        nextButton.backgroundColor = UIColor.red
        nextButton.setTitle("YourButtonTitle", for: .normal)
        nextButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        self.view.addSubview(nextButton)

    


    @objc func buttonAction(sender: UIButton!) 

        print("ButtonTapped")

【问题讨论】:

你试过导航控制器吗? 不要以为那是我要找的。我已经创建了我想要使用的按钮,并将其放置在屏幕上我想要的位置。 【参考方案1】:

您可以使用setViewControllers(_:animated:) 在控制器之间移动。我建议使用pageViewController:viewControllerBeforeViewController:pageViewController:viewControllerAfterViewController: 来确定要显示哪个控制器,然后使用setViewControllers(_:animated:) 在页面之间手动移动。

【讨论】:

谢谢 Colin,那段代码是要在点击按钮时执行的吗? 抱歉耽搁了,我会试一试告诉你。

以上是关于无法连接页面视图控制器上的按钮并使用按钮滑动到下一个屏幕的主要内容,如果未能解决你的问题,请参考以下文章

所有视图上的页面视图控制器按钮

iPhone 键盘快速点击“下一步”按钮

如何在我的应用程序中使用 Facebook 登录并在成功登录后移至下一页/视图控制器?

是否无法在 UITableViewCell 上的删除操作(滑动按钮)中使用自定义视图?

杀死所有视图/控制器并默认为主页按钮上的登录页面不适用于 Web 服务调用

转到ViewPager中的下一页