我使用此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添加操作以更改页面控件中的页面吗? 非常感谢
答案 0 :(得分:0)
在此处设置代表
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
}
}
}
答案 1 :(得分:0)
@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
}
}
}