ios – Swift:类型’ViewController’不符合协议’UIPageViewControllerDataSource’

前端之家收集整理的这篇文章主要介绍了ios – Swift:类型’ViewController’不符合协议’UIPageViewControllerDataSource’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Xcode 6 GM.我正在尝试实现这个 page view controller tutorial但是在Swift中而不是Objective-C但它没有按预期工作.

我实际上设法找到了其他人正在做同样的git repo,但是在克隆他们的项目并在Xcode中打开它之后,我遇到了同样的错误.在实现UIPageViewControllerDataSource协议时,除了协议一致性问题之外,我已经设法解决了大部分问题.

说实话,我不完全明白其用法?而且!在斯威夫特,如果这是导致我的问题.删除!从协议的方法实现中的变量引起其他错误.

有人可以帮忙吗?

class ViewController: UIViewController,UIPageViewControllerDataSource {

var pageViewController : UIPageViewController?
var pageTitles = ["Over 200 Tips and Tricks","Discover Hidden Features","Bookmark Favorite Tip","FreeRegular Update"]
var pageImages = ["page1.png","page2.png","page3.png","page4.png"]
var currentIndex = 0

@IBAction func startWalkthrough(sender: UIButton) {
    var startingViewController : PageContentViewController = self.viewControllerAtIndex(0)!
    var viewControllers : NSArray = [startingViewController]
    self.pageViewController!.setViewControllers(viewControllers,direction: .Forward,animated: false,completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    //Create page view controller
    self.pageViewController = UIPageViewController(transitionStyle: .Scroll,navigationOrientation: .Horizontal,options: nil)
    self.pageViewController!.dataSource = self

    let startingViewController : PageContentViewController = self.viewControllerAtIndex(0)!
    let viewControllers: NSArray = [startingViewController]
    self.pageViewController!.setViewControllers(viewControllers,completion: nil)

    // Change the size of page view controller
    self.pageViewController!.view.frame = CGRectMake(0,self.view.frame.size.width,self.view.frame.size.height - 30);

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

}

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

        var index = (viewController as PageContentViewController).pageIndex

        if index == 0 || index == NSNotFound {
            return nil
        }

        index!--

        println("Decreasing Index: \(index)")

        return self.viewControllerAtIndex(index!)
}

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

        var index = (viewController as PageContentViewController).pageIndex

        if index == NSNotFound {
            return nil
        }

        index!++

        println("Increasing Index: \(index)")

        if index == self.pageTitles.count {
            return nil;
        }
        return self.viewControllerAtIndex(index!);
}

func viewControllerAtIndex(index : Int) -> PageContentViewController? {

    if self.pageTitles.count == 0 || index >= self.pageTitles.count {
        return nil;
    }

    // Create a new view controller and pass suitable data.
    let pageContentViewController = self.storyboard!.instantiateViewControllerWithIdentifier("PageContentViewController") as PageContentViewController
    pageContentViewController.imageFile = self.pageImages[index]
    pageContentViewController.titleText = self.pageTitles[index]
    pageContentViewController.pageIndex = index

    return pageContentViewController;
}

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

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

解决方法

这是因为UIPageViewControllerDataSource协议具有更新的方法签名 – 您正在使用:
func pageViewController(pageViewController: UIPageViewController!,viewControllerBeforeViewController viewController: UIViewController!) -> UIViewController!

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

但现在它们是:

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

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

当您对不符合协议有疑问时,命令单击协议名称将带您进入协议声明,您可以在其中查看是否正确实现其接口

猜你在找的iOS相关文章