简体   繁体   English

使用 UIPageViewController 的一些错误

[英]some error with using UIPageViewController

I tying to use this code to create reader like any ebook readers with using UIPageViewController but I have three errors in code (marked with comments):我想使用此代码来创建阅读器,就像使用 UIPageViewController 的任何电子书阅读器一样,但我在代码中出现了三个错误(用注释标记):

  1. Expression resolves to an unused property Initializer for conditional表达式解析为一个未使用的属性初始化器用于条件
  2. binding must have Optional type, not 'TextDataViewController' 'nil' is绑定必须有 Optional 类型,而不是 'TextDataViewController' 'nil' 是
  3. incompatible with return type 'TextDataViewController'与返回类型“TextDataViewController”不兼容

How to fix it?如何解决?

This is my code:这是我的代码:

class ReadViewController: UIViewController, NSLayoutManagerDelegate {
    
    @IBOutlet weak var contentView: UIView!
    let dataSource = ["1","2","3","4"]
    var currentViewControllerIndex = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configurePageViewController()
    }
    
    func configurePageViewController() {
        guard let pageViewController = storyboard?.instantiateViewController(withIdentifier: String(describing: TextPageViewController.self)) as? TextPageViewController else {
            return
        }
        
        pageViewController.delegate.self // error: Expression resolves to an unused property 
        pageViewController.dataSource .self // error: Expression resolves to an unused property 

        
        addChildViewController(pageViewController)
        pageViewController.didMove(toParentViewController: self)
        
        pageViewController.view.translatesAutoresizingMaskIntoConstraints = false
        
        contentView.addSubview(contentView)
        
        let views: [String: Any] = ["pageView": pageViewController.view]
        
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[pageView]-0-|",
                                                                 options: NSLayoutConstraint.FormatOptions(rawValue: 0),
                                                                 metrics: nil,
                                                                 views: views))
        
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[pageView]-0-|",
                                                                 options: NSLayoutConstraint.FormatOptions(rawValue: 0),
                                                                 metrics: nil,
                                                                 views: views))
        
        guard let startingViewController = detailViewControllerAt(index: currentViewControllerIndex) else{  // error: Initializer for conditional binding must have Optional type, not 'TextDataViewController'

            return
        }
        
        pageViewController.setViewControllers([startingViewController]?, direction: .forward, animated: true)
    }
    
    func detailViewControllerAt(index: Int) -> TextDataViewController {
        
        if index >= dataSource.count || dataSource.count == 0 {
            return nil // error: 'nil' is incompatible with return type 'TextDataViewController'
        }
        
        guard let dataViewController = storyboard?.instantiateViewController(withIdentifier: String(describing: TextDataViewController.self)) as? TextDataViewController else {
            return nil // error: 'nil' is incompatible with return type 'TextDataViewController'
        }
        
        dataViewController.index = index
        dataViewController.displayText = dataSource[index]
        
        return dataViewController
    }
    
}

extension ReadViewController: UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {

        print ("presentationIndex \(currentViewControllerIndex)")

        return currentViewControllerIndex
    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int {

        print ("presentationCount \(dataSource.count)")

        return dataSource.count
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let dataViewController = viewController as? TextDataViewController

        guard var currentIndex = dataViewController?.index else {
            return nil
        }

        print ("viewControllerBefore \(currentIndex)")

        if (currentIndex == 0) {
            return nil
        }

        currentIndex -= 1

        currentViewControllerIndex = currentIndex

        return detailViewControllerAt(index: currentIndex)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let dataViewController = viewController as? TextDataViewController

        guard var currentIndex = dataViewController?.index else {
            return nil
        }

        print ("viewControllerBefore \(currentIndex)")

        if currentIndex == dataSource.count-1 {
            return nil
        }

        currentIndex += 1

        currentViewControllerIndex = currentIndex

        return detailViewControllerAt(index: currentIndex)
    }
}

Change your code like below更改您的代码,如下所示

pageViewController.delegate = self
pageViewController.dataSource = self
func detailViewControllerAt(index: Int) -> TextDataViewController? {

You are trying to return nil but your function return type is not nullable, or Optional in Swift.您正在尝试返回nil但您的函数返回类型不可为空,或者在 Swift 中为Optional

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM