【问题标题】:How to Reset/reload the UI Page Controller views in swift [duplicate]如何快速重置/重新加载 UI 页面控制器视图 [重复]
【发布时间】:2018-07-22 17:31:30
【问题描述】:

我正在寻找如何重新加载 UI 页面控制器。 在表 VC 上,我添加了 UIHeaderView 并嵌入了 UIPage VC。 Page VC 正在从 firebase 动态加载图像。 第一次加载正确的图像集,但是当我在表 VC 上选择其他 cel 时,Page VC 需要刷新其数据。 如何实现这个功能 这是我为 PAGE VC 写的 coed

//-----PAGE VC CODE------
import UIKit
import Firebase

protocol ProductImagesPageVCDelegate: class
{
    func setupPageController(numberOfPages: Int)
    func turnPageController(to index: Int)
}

class ProductImagesPageVC: UIPageViewController {

    var product: Product!

    weak var pageViewControllerDelegate: ProductImagesPageVCDelegate?

    struct StoryBoard {
        static let productImageVC = "ProductImageVC"
    }

    lazy var controllers: [UIViewController] = {

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        var controllers = [UIViewController]()

        if let imageLinks = self.product.imageLinks
        {
            for imageLink in imageLinks
            {
                let productImageVC = storyboard.instantiateViewController(withIdentifier: StoryBoard.productImageVC)
                controllers.append(productImageVC)
            }
        }

        self.pageViewControllerDelegate?.setupPageController(numberOfPages: controllers.count)

        return controllers
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

//        if #available(iOS 11.0, *) {
//            contentInsetAdjustmentBehavior = .never
//        } else {
//            automaticallyAdjustsScrollViewInsets = false
//        }
        automaticallyAdjustsScrollViewInsets = false
        dataSource = self
        delegate = self

        self.turnToPage(index: 0)


    }


    func turnToPage(index: Int)
    {
        let controller = controllers[index]
        var direction = UIPageViewControllerNavigationDirection.forward

        if let currentVC = viewControllers?.first
        {
            guard let currentIndex = controllers.index(of: currentVC) else {return}
            if currentIndex > index
            {
                direction = .reverse
            }
        }

        self.configuewDisplaying(viewController: controller)

        setViewControllers([controller], direction: direction, animated: true, completion: nil)

    }

    func configuewDisplaying(viewController: UIViewController)
    {
        for (index, vc) in controllers.enumerated()
        {
            if viewController === vc {
                if let productImageVC = viewController as? ProductImageVC
                {
                    productImageVC.imageLink = self.product.imageLinks?[index]

                    self.pageViewControllerDelegate?.turnPageController(to: index)
                }
            }
        }
    }


}

extension ProductImagesPageVC: UIPageViewControllerDataSource
{
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        if let index = controllers.index(of: viewController)
        {
            if index < controllers.count - 1
            {
                return controllers[index + 1]
            }
        }


        return controllers.first
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        if let index = controllers.index(of: viewController)
        {
            if index > 0
            {
                return controllers[index - 1]
            }
        }

        return controllers.last
    }
}

extension ProductImagesPageVC: UIPageViewControllerDelegate
{

    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {

        self.configuewDisplaying(viewController: pendingViewControllers.first as! ProductImageVC)
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {

        if !completed
        {
            self.configuewDisplaying(viewController: previousViewControllers.first as! ProductImageVC)
        }
    }



}

【问题讨论】:

  • 我已经尝试了一切都不起作用。因为它们被称为目标 c 或旧的 swift 版本

标签: ios swift uipageviewcontroller


【解决方案1】:

试试这个-

让 viewControllers: [UIViewControllers] = [UIViewController()]

如果让 pageViewController = parentViewController 为? UIPageViewController { pageViewController.setViewControllers(viewControllers, 方向: .Forward, 动画: true, 完成: nil)}

【讨论】:

    猜你喜欢
    • 2014-11-13
    • 2015-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    相关资源
    最近更新 更多