从数组中获取值以在UIPageViewController中显示

时间:2018-03-28 07:16:06

标签: ios

我的代码如下: -

import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


class ViewController: UIViewController, UIPageViewControllerDataSource {

    // MARK: - Variables
    fileprivate var pageViewController: UIPageViewController?

    let name = ["dfyguhijo","drtfyguhijopk","srdtfyguio"]

    // Initialize it right away here
    fileprivate let contentImages = ["nature_pic_1",
                                 "nature_pic_2",
                                 "nature_pic_3",
                                 "nature_pic_4"]

    // MARK: - View Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        createPageViewController()
        setupPageControl()
    }

    fileprivate func createPageViewController() {

        let pageController = self.storyboard!.instantiateViewController(withIdentifier: "PageController") as! UIPageViewController
        pageController.dataSource = self

        if name.count > 0 {
            let firstController = getItemController(0)!
            let startingViewControllers = [firstController]
            pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
        }

        pageViewController = pageController
        addChildViewController(pageViewController!)
        self.view.addSubview(pageViewController!.view)
        pageViewController!.didMove(toParentViewController: self)
    }

    fileprivate func setupPageControl() {
        let appearance = UIPageControl.appearance()
        appearance.pageIndicatorTintColor = UIColor.gray
        appearance.currentPageIndicatorTintColor = UIColor.white
        appearance.backgroundColor = UIColor.darkGray
    }

    // MARK: - UIPageViewControllerDataSource

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

        let itemController = viewController as! PageItemController

        if itemController.itemIndex > 0 {
            return getItemController(itemController.itemIndex-1)
        }

        return nil
    }

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

        let itemController = viewController as! PageItemController

        if itemController.itemIndex+1 < name.count {
            return getItemController(itemController.itemIndex+1)
        }

        return nil
    }

    fileprivate func getItemController(_ itemIndex: Int) -> PageItemController? {

        if itemIndex < contentImages.count {
            let pageItemController = self.storyboard!.instantiateViewController(withIdentifier: "ItemController") as! PageItemController
            pageItemController.itemIndex = itemIndex
            pageItemController.imageName = contentImages[itemIndex]
          //  pageItemController.namess = [name[itemIndex]]
            return pageItemController
        }

        return nil
    }

    // MARK: - Page Indicator

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return name.count
    }

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

    // MARK: - Additions

    func currentControllerIndex() -> Int {

        let pageItemController = self.currentController()

        if let controller = pageItemController as? PageItemController {
            return controller.itemIndex
        }

        return -1
    }

    func currentController() -> UIViewController? {

        if self.pageViewController?.viewControllers?.count > 0 {
            return self.pageViewController?.viewControllers![0]
        }

        return nil
    }

}

和pageitem控制器: -

    import UIKit

class PageItemController: UIViewController {

    let namess = ["ram","sam","geera`enter code here`"]



    // MARK: - Variables
    var itemIndex: Int = 0
    var imageName: String = "" {

        didSet {

            if let imageView = contentImageView {
                imageView.image = UIImage(named: imageName)
            }

        }
    }
    @IBOutlet weak var name: UILabel!

    var names:String!
    var des:String!

    @IBOutlet weak var desc: UILabel!
    @IBOutlet var contentImageView: UIImageView?


    // MARK: - View Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        contentImageView!.image = UIImage(named: imageName)
        //name.text = namess[0]
       // desc.text = "srdtyugiop fhgkl;"
    }
}

这是UIPageViewController。在这里我得到了输出。但我需要显示存储在数组中的第一个名称应首先显示然后当页面控制为2时,名称应显示索引-1。然后在第三页它应显示存储在索引中的名称 - 2,怎么做

1 个答案:

答案 0 :(得分:0)

for i in 0..<names.count {
  let name = names[i]
}