使用UIPageViewController与快速和多个视图控制器 [英] Using UIPageViewController with swift and multiple view controllers

查看:91
本文介绍了使用UIPageViewController与快速和多个视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在具有多个视图控制器的Swift应用程序中使用 UIPageViewController 。我的故事板上有2个视图控制器( firstViewController secondViewController ),它们都嵌入在自己的导航控制器中。他们有故事板标识符 FirstViewController SecondViewController 。我的故事板上还有一个页面视图控制器,带有 identifierPageView 控制器,并将导航设置为水平,过渡样式在属性检查器中滚动。 FirstViewController 是应用程序的根视图控制器

I'm trying to utilize a UIPageViewController in a Swift app with multiple view controllers. I have 2 view controllers on my storyboard (firstViewController and secondViewController) that are both embedded in their own navigation controllers. They have storyboard identifiers "FirstViewController" and "SecondViewController". I also have a page view controller on my storyboard with identifierPageView controller and set the navigation to horizontal and transition style to scroll in the attributes inspector. FirstViewController is the root view controller of the app

我想 firstViewController 成为页面视图控制器的第一页,所以我写了这样的类:

I want firstViewController to be the first page of the page view controller so I have written its class like so:

import Foundation
import UIKit

class FirsttViewController: UITableViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate  {

    override func viewDidLoad() {

        let pageViewController: PageViewController = self.storyboard.instantiateViewControllerWithIdentifier("PageViewController") as PageViewController

        pageViewController.delegate = self

        var viewControllers: [UIViewController] = [self]

        pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
        pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height)

        self.addChildViewController(pageViewController)
        self.view.addSubview(pageViewController.view)
        pageViewController.didMoveToParentViewController(self)


    }
    func pageViewController(pageViewController: UIPageViewController!, viewControllerAfterViewController viewController: UIViewController!) -> UIViewController! {


        let secondViewController: SecondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController


        return secondViewController
    }

    func pageViewController(pageViewController: UIPageViewController!, viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! {

        return nil
    }

    func presentationCountForPageViewController(pageViewController: UIPageViewController!) -> Int {
        return 2
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController!) -> Int {
        return 0
    }
}

SecondViewController 如下所示:

import Foundation
import UIKit

class SecondViewController: UITableViewController, UIPageViewControllerDataSource {

    func pageViewController(pageViewController: UIPageViewController!, viewControllerAfterViewController viewController: UIViewController!) -> UIViewController! {

        return nil
    }

    func pageViewController(pageViewController: UIPageViewController!, viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! {

        let firstViewController: FirstViewController = self.storyboard.instantiateViewControllerWithIdentifier("FirstViewController") as FirstViewController
        return firstViewController
    }

}

然而,当我运行我的应用程序时,似乎没有页面视图控制器。它只显示 firstViewController 就是这样。没有点,没有页面等。任何人都知道什么是错的?我发现了几个Objective-C教程,但它们都覆盖了所有页面使用一个视图控制器,并且有一个根视图控制器,它不是 pageView 中的页面所以他们没有太多的帮助。我希望它会像使用 tabBarController 一样,你只需点击并拖动到视图控制器,但不幸的是不是这样。就像我说的那样我之前没有使用其中一种,所以我有点迷失。

However, when I run my app it doesn't seem to have a page view controller. It just shows firstViewController and that's it. No dots, no pages, etc. Anyone know whats wrong? I've found a couple of Objective-C tutorials but they all cover using one view controller for all the pages and have a root view controller that is not a page in the pageView so they weren't too much help. I was hoping it was going to be like using a tabBarController where you just click and drag to the view controllers but thats not the case unfortunately. like I said I've not worked with one of these before so I'm sort of lost.

根据iiFreeman的建议,我将 UIPageViewController 子类化,并将其作为故事板文件的根目录。下面是子类

following iiFreeman's suggestion, I subclassed UIPageViewController and made it the root in my storyboard file. below is the subclass

import Foundation
import UIKit
class PageViewController: UIPageViewController {

    override func viewDidLoad() {

        let startingViewController = self.storyboard.instantiateViewControllerWithIdentifier("FirstViewController") as FirstViewController

        self.dataSource = startingViewController
        var viewControllers: [UIViewController] = [startingViewController]

        self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)



    }

}

但是现在应用程序只挂在黑屏,最终Xcode失去了与模拟器的连接。我不确定是什么。

However now the app just hangs on a black screen and eventually Xcode loses connection to the simulator. I'm not really sure what's up.

推荐答案

好的,我明白了。因为我需要页面视图控制器作为根并处理所有内容,所以我将 UIPageViewController 子类化为iiFreeman建议并符合委托和数据源协议。然后,我在 viewDidLoad 中设置控制器,并实现了委托和数据源方法。我添加了一个属性存储库,其中包含一个故事板标识符数组,以及一个用于跟踪该数组当前索引的标识符。我还添加了一个辅助方法,在给定索引的情况下返回正确的视图控制器。我的其他 tableView 控制器子类中没有任何需要,因为我的页面视图控制器正在处理所有内容。有一点我意识到是因为我的 tableViewController 被嵌入到导航控制器中,我的页面视图控制器实际上需要显示导航控制器,而不是 tableView 控制器。下面是我的实现:

OK, I got it figured out. since I needed the page view controller to be the root and handle everything, I subclassed UIPageViewControlleras iiFreeman suggested and conformed to the delegate and data source protocols. I then set up the controller in viewDidLoad, and implemented the delegate and data source methods. I added a property store an array of storyboard identifiers as well as one to keep track of the current index of that array. I also added a helper method to return the correct view controller given an index. I didn't need anything in my other tableView controller subclasses since my page view controller was handling everything. One thing I did realize is since my tableViewControllers were embedded in navigation controllers, my page view controller actually needed to show the navigation controllers, not the tableView controllers. below is my implementation:

import Foundation
import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    var index = 0
    var identifiers: NSArray = ["FirstNavigationController", "SecondNavigationController"]
    override func viewDidLoad() {

        self.dataSource = self
        self.delegate = self

        let startingViewController = self.viewControllerAtIndex(self.index)
        let viewControllers: NSArray = [startingViewController]
        self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)



    }

    func viewControllerAtIndex(index: Int) -> UINavigationController! {

        //first view controller = firstViewControllers navigation controller
        if index == 0 {

            return self.storyboard.instantiateViewControllerWithIdentifier("FirstNavigationController") as UINavigationController

        }

        //second view controller = secondViewController's navigation controller
        if index == 1 {

            return self.storyboard.instantiateViewControllerWithIdentifier("SecondNavigationController") as UINavigationController
        }

        return nil
    }
    func pageViewController(pageViewController: UIPageViewController!, viewControllerAfterViewController viewController: UIViewController!) -> UIViewController! {

        let identifier = viewController.restorationIdentifier
        let index = self.identifiers.indexOfObject(identifier)

        //if the index is the end of the array, return nil since we dont want a view controller after the last one
        if index == identifiers.count - 1 {

            return nil
        }

        //increment the index to get the viewController after the current index
        self.index = self.index + 1
        return self.viewControllerAtIndex(self.index)

    }

    func pageViewController(pageViewController: UIPageViewController!, viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController! {

        let identifier = viewController.restorationIdentifier
        let index = self.identifiers.indexOfObject(identifier)

        //if the index is 0, return nil since we dont want a view controller before the first one
        if index == 0 {

            return nil
        }

        //decrement the index to get the viewController before the current one
        self.index = self.index - 1
        return self.viewControllerAtIndex(self.index)

    }


    func presentationCountForPageViewController(pageViewController: UIPageViewController!) -> Int {
        return self.identifiers.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController!) -> Int {
        return 0
    }

}

这篇文章帮助了我很多

这篇关于使用UIPageViewController与快速和多个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆