【问题标题】:Swift pageViewController activated by buttonSwift pageViewController 由按钮激活
【发布时间】:2015-02-06 01:42:43
【问题描述】:

在主视图控制器中,我有一个简单的按钮来激活一个pageViewController。这个pageViewController 包含两个视图控制器。

我正在尝试激活它,但它不起作用。

文件pageviewcontroller中的代码是:

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    var myViewControllers : [UIViewController] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.delegate = self
        self.dataSource = self

        let p1 = storyboard?.instantiateViewControllerWithIdentifier("a") as aViewController
        let p2 = storyboard?.instantiateViewControllerWithIdentifier("b") as bViewController

        myViewControllers = [p1,p2]


        for var index = 0; index < myViewControllers.count; ++index {
            NSLog("\(myViewControllers[index])")
        }

        let startingViewController = self.viewControllerAtIndex(0)
        let viewControllers: NSArray = [startingViewController]

        self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: {(done: Bool) in
        })

        NSLog("loaded!");

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //Private Function
    func viewControllerAtIndex (index: NSInteger) -> UIViewController{
        return myViewControllers[index]
    }

    //Delegates and Datasource
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

        NSLog("Entered to BeforeViewController")

        var index = find(myViewControllers, viewController)!
        NSLog("\(index)")
        if (index == 0) || (index == NSNotFound) {
            return nil
        }
        index--
        if index == myViewControllers.count {
            return nil
        }
        NSLog("\(index)")
        return self.viewControllerAtIndex(index)
    }

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

         NSLog("Entered to BeforeViewController")

        var index = find(myViewControllers, viewController)!
        NSLog("\(index)")
        if index == NSNotFound {
            return nil
        }
        index++
        if index == myViewControllers.count {
            return nil
        }
        NSLog("\(index)")
        return self.viewControllerAtIndex(index)

    }

    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return myViewControllers.count
    }

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

pageviewcontroller 包含的视图控制器中的代码是:

import UIKit

class aViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

} 

当我按下按钮时出现此错误:

2015-02-05 20:37:21.111 Tutorial-JSON-Alamofire[616:165935] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x1452de40>) doesn't contain a view controller with identifier 'a''
*** First throw call stack:
(0x21a4a49f 0x2f204c8b 0x2545ea09 0x107130 0x1085e4 0x24f0b52f 0x24f0b29d 0x255645d5 0x251d4ead 0x251ece21 0x251eedb7 0x24feb2ef 0x251f1815 0x25304321 0x24f3c21b 0x24f3c1c1 0x24f26e33 0x24f3bc2d 0x24f3b907 0x24f351d1 0x24f0b97d 0x2517f2e9 0x24f0a3d9 0x21a10d57 0x21a10167 0x21a0e7cd 0x2195c3c1 0x2195c1d3 0x28d5a0a9 0x24f6a7b1 0x10dd60 0x10dd9c 0x2f784aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

【问题讨论】:

    标签: swift uiviewcontroller uipageviewcontroller


    【解决方案1】:

    来自给定的异常:

    您用于检索 UIViewController 引用的标识符不属于 StoryBoard 中的任何标识符。您需要将情节提要 ID 更改为“a”。

    更新

    • 从情节提要中选择您的视图控制器。
    • 从实用程序(右上角)中选择身份检查器(左起第三个)。
    • 在此处输入标识符。

    【讨论】:

    • 我如何从情节提要中做到这一点?
    • @RenzoG 看看这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多