当前位置: 首页 > 面试题库 >

将UIPageViewController与Swift和多个视图控制器一起使用

秦信瑞
2023-03-14
问题内容

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

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

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您只需单击并拖动到视图控制器的按钮,但是不幸的是事实并非如此。就像我说过我以前从未与其中之一合作过,所以我有点迷路了。

更新:

根据iiFreeman的建议,我继承了子类UIPageViewController,并将其作为情节提要文件的根。下面是子类

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失去了与模拟器的连接。我不太确定怎么回事。


问题答案:

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

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
    }

}


 类似资料:
  • 问题内容: 我没有找到有关此问题的文章,但没有一个解决我的问题。 就像我说的那样。 ViewControllerA ViewControllerB 我试图将添加为的子视图,但是它 抛出类似“ ” 的错误。 下面是代码… ViewControllerA ViewControllerB只是一个带有标签的简单屏幕。 ViewControllerB EDIT 根据用户答案的​​建议解决方案,ViewCon

  • 我找到了一些关于这个问题的帖子,但是没有一个能解决我的问题。 说像我已经... ViewControlllerA ViewControlllerB 我试图将ViewControlllerB添加为ViewControlllerA中的子视图,但是,它抛出了一个错误,如""。 下面是代码。。。 视图控制器 ViewControllerB只是一个带有标签的简单屏幕。 视图控制器B 编辑

  • 问题内容: 因此,我有一个带有按钮的根视图控制器,当用户按下该按钮时,将显示另一个视图控制器。第二个控制器具有一个关闭选项,该选项仅返回到根视图控制器,还有一个按钮,当用户触摸它时,该按钮将关闭当前视图控制器,以便它再次返回到根视图控制器并显示另一个按钮。转到我使用的第一个控制器: 在另一个视图控制器中,我选择仅关闭的按钮,然后执行此操作。 因此,对于需要解雇并显示另一个控制器的第二个控制器,我尝

  • 问题内容: 我正在使用Swift,在tableview的didSelectRowAtIndexPath方法中出现错误。我想将值传递给另一个视图控制器,即“ secondViewController”。这里,EmployeesId是一个数组。相关代码如下: 但是我收到此错误:致命错误:展开一个Optional值时意外发现nil。 任何帮助将不胜感激。 问题答案: 这是一个有两个假设的一般解决方案。首

  • 问题内容: 我想使用在视图中定义的一个控制器,但是没有定义任何东西。有没有办法做到这一点?请分享一个简单的例子以了解。 我有这个index.html 这是我的模型定义 app.js 控制台为空,并且html模板中的作用域为空。如果您有解决方案,请使用非常简单的示例。 我在这里阅读,我认为它起作用Rakrap Jaknap于2015-04-17 08:01回答 问题答案: 这里的重点是: 控制器始终

  • 我已经通读了《iOS View Controller编程指南》和《iOS View编程指南》,如果代码或情节提要中没有大量复制,我无法找到执行以下操作的最佳方法。 我正在为我的孩子们编写一个简单的数学教程程序,其中有一个带有数字“NumberPad”(数字0-9)的视图,以及一些动画的场景。 到目前为止,我有一个视图控制器,它处理简单的加减乘除问题,工作良好。 我想为一个视图控制器使用相同的数字键