UIPageViewController + 火力基地

UIPageViewController + firebase

我正在使用 UIPageViewController 制作书页,这些书页是将从 firebase 中获取的图像。问题是我得到了这个错误

(无法将类型 'books' 的值转换为预期的元素类型 'Array.ArrayLiteralElement'(又名 'UIViewController')

'books' 是我为 firebase 制作的结构


import Foundation
import Firebase

struct books {
    
    let bookId: String 
    let imageURL: String
}

extension booksC {
  init(_ dictionary: [String: Any]) {
    
    self.bookId = dictionary["bookId"] as? String ?? "no book id"
    self.imageURL = dictionary["imageURL"] as? String ?? "no image url"
  }
}

我正在使用这段代码,我的理解是我不能使用一组图像,我应该使用一组控制器,但在这种情况下我不知道该怎么做。感谢帮助

import UIKit
import FirebaseFirestore


class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate  {

        let pageViewController = UIPageViewController(transitionStyle: .pageCurl, navigationOrientation: .horizontal, options: nil)


        private var pages  = [books]()
    
        private var listener: ListenerRegistration?

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

        // index

            guard  let pagesFirst = pages.last else { return }

            setViewControllers([pagesFirst], direction: .reverse, animated: true)
      
        //TODO: add listener


        }

        func pageViewController(_ pageViewController: UIPageViewController, spineLocationFor orientation: UIInterfaceOrientation) -> UIPageViewController.SpineLocation {
            var location = SpineLocation.max
            if orientation == .landscapeLeft || orientation == .landscapeRight {
                self.pageViewController.isDoubleSided = true

                location = .mid
            }

            return location
        }

        func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
            let index = self.pages.firstIndex(where: {[=11=] == viewController}) ?? 0
            if index == 0 { return nil }
            return pages[index - 1]

        }

        func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
            let index = self.pages.firstIndex(where: {[=11=] == viewController}) ?? 0
            if index == pages.count - 1 { return nil }
            return pages[index + 1]
        }

    }

class ImageViewController: UIViewController {


    let imageView = UIImageView()
    init(image: UIImage) {
        imageView.image = image
        super.init(nibName: nil, bundle: nil)
    }



    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(imageView)
        imageView.fillSuperview()
        imageView.contentMode = .scaleAspectFit
        imageView.clipsToBounds = true
        imageView.backgroundColor = .white


    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public func setupImage(for book: books){
    imageView.kf.setImage(with: URL(string: book.imageURL))
}

}

enter code here您正在 setViewControllers 中使用您的数组 private var pages = [books](),它期望 UIViewController

这是 setViewControllers 的声明方式:

func setViewControllers(_ viewControllers: [UIViewController]?, 
              direction: UIPageViewController.NavigationDirection, 
               animated: Bool, 
             completion: ((Bool) -> Void)? = nil)

参数


视图控制器

要显示的一个或多个视图控制器。

方向

导航方向。

动画

一个布尔值,指示过渡是否要设置动画。

我认为你应该创建一个 ImageViewController 的实例,然后使用 setupImage(for book: books) 设置图像(你可以访问它,因为它是一个 public 方法,最后将该视图控制器分配给你的页面控制器:

guard  let pagesFirst = pages.last else { return }
let imageViewController = ImageViewController()
imageViewController.setupImage(for book: pagesFirst)
setViewControllers([imageViewController], direction: .reverse, animated: true)

已编辑:

抱歉,就您得到的第二个错误而言,应该是

 imageViewController.setupImage(for:pagesFirst)

至于是class之外可用的名字。

对于第一个错误,它为您提供了哪些初始化选项。应该是正确的。

也尝试更改以这种方式声明 imageView 的代码:

var imageView : UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false // required if you need to work with autolayout
    return imageView
}()

删除:

 required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

最后,你应该用大写字母 B 命名你的结构 book