gpt4 book ai didi

ios - 滑动 PageViewController 时如何在 ViewController 中更新 UIPageControl

转载 作者:行者123 更新时间:2023-12-04 07:35:20 26 4
gpt4 key购买 nike

我有三个类OnboardingViewController , OnboardingPageViewController , 和 OnboardingContentViewController .
在我的 ViewController 类中,我有一个 UIPageControl每当我按下 UIButton 时都会更新在它下面的“下一个”。这是按预期工作的。但是我想制作 UIPageControl根据我在 UIPageViewController 上向左/向右滑动时更新,但我不知道该怎么做。

class OnboardingViewController: UIViewController {

@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var pageControl: UIPageControl!

var onboardingPageViewController: OnboardingPageViewController?

@IBAction func nextButtonTapped(_ sender: UIButton) {
if let index = onboardingPageViewController?.currentPageIndex {
switch index {
case 0:
onboardingPageViewController?.nextPage()
case 1:
onboardingPageViewController?.nextPage()
nextButton.setTitle("Get Started", for: .normal)
case 2:
//present login ViewController after final next button is tapped
default:
break
}
pageControl.currentPage = index + 1
}
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination
if let pageViewController = destination as? OnboardingPageViewController {
onboardingPageViewController = pageViewController
}
}
}
class OnboardingContentViewController: UIViewController {
@IBOutlet var imageView: UIImageView!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var introLabel: UILabel!

var index = 0
var contentTitle = ""
var intro = ""
var imageFile = ""

override func viewDidLoad() {
super.viewDidLoad()

titleLabel.text = contentTitle
introLabel.text = intro
imageView.image = UIImage(named: imageFile)
}
}
class OnboardingPageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var pageTitle = ["Title1", "Title2", "Title3"]
var pageIntro = ["Description1","Description2","Description3"]
var pageImages = ["image1","image2","image3"]
var currentPageIndex = 0

override func viewDidLoad() {
super.viewDidLoad()

dataSource = self
delegate = self

if let startingViewController = contentViewController(at: currentPageIndex) {
setViewControllers([startingViewController], direction: .forward,animated: true, completion: nil)
}
}

func nextPage() {
currentPageIndex += 1
if let nextViewController = contentViewController(at: currentPageIndex){
setViewControllers([nextViewController], direction:.forward, animated: true, completion:nil)
}
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index = (viewController as! OnboardingContentViewController).index
index -= 1
return contentViewController(at:index)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index = (viewController as! OnboardingContentViewController).index
index += 1
return contentViewController(at:index)
}

func contentViewController(at index: Int) -> OnboardingContentViewController? {
if index < 0 || index >= pageIntro.count {
return nil
}

let storyboard = UIStoryboard(name: "Onboarding", bundle: nil)
if let pageContentViewController = storyboard.instantiateViewController(withIdentifier:"OnboardingContentViewController") as? OnboardingContentViewController {
pageContentViewController.imageFile = pageImages[index]
pageContentViewController.contentTitle = pageTitle[index]
pageContentViewController.intro = pageIntro[index]
pageContentViewController.index = index
return pageContentViewController
}
return nil
}

}
我尝试将 UIPageViewController 委托(delegate)方法添加到第一个 View Controller ,但我发现控制台中没有打印任何内容。我错过了什么?
class OnboardingViewController: UIViewController, UIPageViewControllerDelegate {

@IBOutlet weak var pageControl: UIPageControl!

override func viewDidLoad() {
super.viewDidLoad()

let pageController = self.storyboard!.instantiateViewController(withIdentifier: "OnboardingPageViewController") as! UIPageViewController
pageController.delegate = self

}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed == true {
print("change page control number")
}
}
enter image description here

最佳答案

当你这样做 let pageController = self.storyboard!.instantiateViewController... ,您正在初始化页面 View Controller 的新副本。这与您的用户看到的无关。

/// no!
let pageController = self.storyboard!.instantiateViewController(withIdentifier: "OnboardingPageViewController") as! UIPageViewController
pageController.delegate = self
相反,您需要引用 Storyboard中添加的现有页面 View Controller 。它被自动添加为 OnboardingViewController 的第一个 child , 所以你可以使用 self.children[0] .
class OnboardingViewController: UIViewController, UIPageViewControllerDelegate {

@IBOutlet weak var pageControl: UIPageControl!

/// here!
var pageController: UIPageViewController {
return self.children[0] as! UIPageViewController
}

override func viewDidLoad() {
super.viewDidLoad()

pageController.delegate = self
}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if completed == true {
print("change page control number")
}
}
}

关于ios - 滑动 PageViewController 时如何在 ViewController 中更新 UIPageControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67776549/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com