从另一个 View Controller 访问页面控件以转发页面

Access to page control from another View Controller to forward the page

我使用此 page 为登录页面制作页面控件。在另一个视图控制器中,我有一个按钮,我希望当用户单击该按钮时,页面控件应转到下一页。 包含此按钮的视图控制器通过 segue 连接到页面控件 假设这是按钮的动作

 @IBAction func pressNextBtn(_ sender: OButton) {

}

这是准备转场

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "welcome" {
        if let welcomePage = segue.destination as? welcomePageViewController {

        }
    }
}

这是页面控制器

class welcomePageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {


    lazy var orderedViewControllers: [UIViewController] = {
    return [self.newVc(viewController: "welcome"), self.newVc(viewController: "apperance")]
}()


func forwardPage(){
    if let firstViewController = orderedViewControllers.first {
        setViewControllers([firstViewController],
                           direction: .forward,
                           animated: true,
                           completion: nil)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    self.delegate = self
    forwardPage()
}





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


func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    let pageContentViewController = pageViewController.viewControllers![0]

}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    guard let viewControllerIndex = orderedViewControllers.firstIndex(of: viewController) else {
        return nil
    }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0 else {
        return nil

    }

    guard orderedViewControllers.count > previousIndex else {
        return nil
    }

    return orderedViewControllers[previousIndex]
}

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

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

    guard orderedViewControllersCount != nextIndex else {
        return nil
    }

    guard orderedViewControllersCount > nextIndex else {
        return nil
    }

    return orderedViewControllers[nextIndex]
}

}

任何人都可以帮助我如何为下一个 btn 添加操作以更改页面控件中的页面吗? 非常感谢

在此处设置委托

weak var delegate:WelcomePageViewController!

 @IBAction func pressNextBtn(_ sender: OButton) {
    // this 
    delegate.setViewControllers([delegate.orderedViewControllers[index]], direction: .forward, animated: true, completion: nil)
    // or
    delegate.goToNext() // and add logic there 
} 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "welcome" {
        if let welcomePage = segue.destination as? WelcomePageViewController {
           delegate = welcomePage
        }
    }
}

@Sh_Khan感谢您的帮助, 这是我最新的代码;与您的代码完全一样:

weak var delegate:WelcomePageViewController!

@IBAction func pressNextBtn(_ sender: OButton) { 
delegate.setViewControllers([delegate.orderedViewControllers[index]], direction: .forward, animated: true, completion: nil)
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "welcome" {
    if let welcomePage = segue.destination as? WelcomePageViewController {
       delegate = welcomePage
    }
}
}