我正在遵循本教程:http://swiftiostutorials.com/ios-tutorial-using-uipageviewcontroller-create-content-slider-objective-cswift/创建一个显示多个滑块的应用程序。

即使我已使本教程正常工作,此示例也仅根据存储在数组中的图像更改图像。

如何获取它来加载ViewController而不是图像

我有4个ViewController:

  • ViewController1
  • ViewController2
  • ViewController3
  • ViewController4

  • 我希望一张幻灯片显示ViewController1,而slide2加载ViewController2等...。

    这是我的主要ViewController:
     import UIKit
    
    class ViewController: UIViewController, UIPageViewControllerDataSource {
    
    // MARK: - Variables
    private var pageViewController: UIPageViewController?
    
    // Initialize it right away here
    private let contentImages = ["nature_pic_1.png",
                                 "nature_pic_2.png",
                                 "nature_pic_3.png",
                                 "nature_pic_4.png"];
    
    // MARK: - View Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        createPageViewController()
        setupPageControl()
    }
    
    private func createPageViewController() {
    
        let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as UIPageViewController
        pageController.dataSource = self
    
        if contentImages.count > 0 {
            let firstController = getItemController(0)!
            let startingViewControllers: NSArray = [firstController]
            pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
        }
    
        pageViewController = pageController
        addChildViewController(pageViewController!)
        self.view.addSubview(pageViewController!.view)
        pageViewController!.didMoveToParentViewController(self)
    }
    
    private func setupPageControl() {
        let appearance = UIPageControl.appearance()
        appearance.pageIndicatorTintColor = UIColor.grayColor()
        appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
        appearance.backgroundColor = UIColor.darkGrayColor()
    }
    
    // MARK: - UIPageViewControllerDataSource
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    
        let itemController = viewController as PageItemController
    
        if itemController.itemIndex > 0 {
            return getItemController(itemController.itemIndex-1)
        }
    
        return nil
    }
    
    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    
        let itemController = viewController as PageItemController
    
        if itemController.itemIndex+1 < contentImages.count {
            return getItemController(itemController.itemIndex+1)
        }
    
        return nil
    }
    
    private func getItemController(itemIndex: Int) -> PageItemController? {
    
        if itemIndex < contentImages.count {
            let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as PageItemController
            pageItemController.itemIndex = itemIndex
            pageItemController.imageName = contentImages[itemIndex]
            return pageItemController
        }
    
        return nil
    }
    
    // MARK: - Page Indicator
    
    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return contentImages.count
    }
    
    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return 0
    }
    

    这是我的PageItemController:
    import UIKit
    
    class PageItemController: UIViewController {
    
    // MARK: - Variables
    var itemIndex: Int = 0
    var imageName: String = "" {
    
        didSet {
    
            if let imageView = contentImageView {
                imageView.image = UIImage(named: imageName)
            }
    
        }
    }
    
    @IBOutlet var contentImageView: UIImageView?
    
    // MARK: - View Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        contentImageView!.image = UIImage(named: imageName)
    }
    }
    

    我是Swift/iOS开发的新手,我真的想通过开发来了解它。预先感谢您的回答:)

    编辑:清除问题

    我如何做到这一点,以便有一个与UIPageViewController的左/右幻灯片相对应的 View Controller 数组?

    因此,当我在ViewController1上向左滑动时-将加载UIViewController2并向右滑动。

    最佳答案

    假设您已在与UIPageViewController相同的 Storyboard 中定义了 View Controller 1-4,并且将其 Storyboard ID设置为ViewController0,ViewController1等,然后创建一种方法来填充 View Controller 数组并在viewDidLoad()中对其进行调用调用createPageViewController()

        override func viewDidLoad() {
            super.viewDidLoad()
            populateControllersArray()
            createPageViewController()
            setupPageControl()
        }
    

    像这样实现方法:
        var controllers = [PageItemController]()
    
        func populateControllersArray() {
            for i in 0...3 {
                let controller = storyboard!.instantiateViewControllerWithIdentifier("ViewController\(i)") as PageItemController
                controller.itemIndex = i
                controllers.append(controller)
            }
        }
    

    并将您的createPageViewController()定义为以下内容
        private func createPageViewController() {
    
            let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as UIPageViewController
            pageController.dataSource = self
    
            if !controllers.isEmpty {
                pageController.setViewControllers([controllers[0]], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
            }
    
            pageViewController = pageController
            addChildViewController(pageViewController!)
            self.view.addSubview(pageViewController!.view)
            pageViewController!.didMoveToParentViewController(self)
        }
    

    然后在您的两个委托(delegate)之前和之后的方法中:
        func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
            if let controller = viewController as? PageItemController {
                if controller.itemIndex > 0 {
                    return controllers[controller.itemIndex - 1]
                }
            }
            return nil
        }
        func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
            if let controller = viewController as? PageItemController {
                if controller.itemIndex < controllers.count - 1 {
                    return controllers[controller.itemIndex + 1]
                }
            }
            return nil
        }
    

    并在计数方法中
        func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
            return controllers.count
        }
    

    实际上,您可以使用要显示的任何 View Controller 填充controllers,只需将其类设置为 Storyboard 中的PageItemController(以具有index属性)即可。

    或者,您可以将每个 View Controller 设置为自己的类,并使用运行时属性获取和设置。

    在before和after方法中使用controller.valueForKey("itemIndex") as Int而不是controller.itemIndex
    使用controller.setValue(i, forKey: "itemIndex")代替controller.itemIndex = i中的populateControllersArray()

    只需确保每个 Controller 类都具有Int属性itemIndex,否则您的应用程序将崩溃。

    要将所有内容整合到您的代码中,请执行以下操作:
    import UIKit
    
    import UIKit
    
    class ViewController: UIViewController, UIPageViewControllerDataSource {
    
        // MARK: - Variables
        private var pageViewController: UIPageViewController?
    
        // MARK: - View Lifecycle
        override func viewDidLoad() {
            super.viewDidLoad()
            populateControllersArray()
            createPageViewController()
            setupPageControl()
        }
    
        var controllers = [PageItemController]()
    
    
        func populateControllersArray() {
            for i in 0...3 {
                let controller = storyboard!.instantiateViewControllerWithIdentifier("ViewController\(i)") as PageItemController
                controller.itemIndex = i
                controllers.append(controller)
            }
        }
    
        private func createPageViewController() {
    
            let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as UIPageViewController
            pageController.dataSource = self
    
            if !controllers.isEmpty {
                pageController.setViewControllers([controllers[0]], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
            }
    
            pageViewController = pageController
            addChildViewController(pageViewController!)
            self.view.addSubview(pageViewController!.view)
            pageViewController!.didMoveToParentViewController(self)
        }
    
        private func setupPageControl() {
            let appearance = UIPageControl.appearance()
            appearance.pageIndicatorTintColor = UIColor.grayColor()
            appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
            appearance.backgroundColor = UIColor.darkGrayColor()
        }
    
        // MARK: - UIPageViewControllerDataSource
        func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
            if let controller = viewController as? PageItemController {
                if controller.itemIndex > 0 {
                    return controllers[controller.itemIndex - 1]
                }
            }
            return nil
        }
        func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
            if let controller = viewController as? PageItemController {
                if controller.itemIndex < controllers.count - 1 {
                    return controllers[controller.itemIndex + 1]
                }
            }
            return nil
        }
    
        // MARK: - Page Indicator
    
        func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
            return controllers.count
        }
    
        func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
            return 0
    }
    

    08-26 04:25