在Swift中使用UIPageViewController的麻烦

时间:2014-12-10 22:48:40

标签: ios swift uiview segue uipageviewcontroller

我尝试使用swift为我的应用创建教程流程非常困难......这就是我想要做的事情: 用户进入启动屏幕,其中有一个按钮,允许他们进行游览"这是故事板,工作正常。 用户按下按钮,从push segue出现教程。 经过教程,用户可以继续执行其他功能。

我首先尝试通过将其拖入故事板并附加一个类来创建UIPageViewController。那不行。所以我找到了一个我用来创建它的教程:

import UIKit

class TutorialViewController: UIViewController, UIPageViewControllerDataSource {

var pageController: UIPageViewController?
let pageAssets:[String] = ["blender.png", "click.png", "share.png"]
var currentIndex: Int = 0

// MARK: - View Lifecycle
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    pageController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
    pageController!.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height);
    pageController!.dataSource = self

    var initialViewController: PageContentViewController = self.viewControllerAtIndex(0)
    let viewControllers: NSArray = [initialViewController]

    pageController!.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)

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

}

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


// MARK: - DataSource protocol conformance
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    var idx: Int = (viewController as PageContentViewController).itemIndex

    if (idx == 0) {
        return nil
    }

    idx--

    return self.viewControllerAtIndex(idx)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    var idx = (viewController as PageContentViewController).itemIndex

    if (idx > pageAssets.count){
        return nil
    }

    idx++

    return self.viewControllerAtIndex(idx)
}


func viewControllerAtIndex(index:Int) -> PageContentViewController {
    let childViewController:PageContentViewController = PageContentViewController()
    childViewController.itemIndex = index
    childViewController.imageName = pageAssets[index]
    currentIndex = index

    return childViewController
}


// MARK: - Navigation
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return pageAssets.count
}

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

/*
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
} */

}

这是PageContentViewController:

import UIKit

class PageContentViewController: UIViewController  {

    @IBOutlet weak var testLabel: UILabel!
    @IBOutlet weak var tutorialImageView: UIImageView!

    // MARK: - Variables
    @IBOutlet weak var contentImageView: UIImageView!
    var itemIndex: Int = 0

    //computed member which calls didSet that actually creates the UIImage that's displayed
    var imageName: String = ""{
        didSet {
            if let imageView = contentImageView {
                tutorialImageView.image = UIImage(named: imageName)
            }
        }
    }

    // MARK: - View Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        contentImageView?.image = UIImage(named: imageName)
    }

}

首先,我如何使用Segue,或从我的AppDelegate进入此类?

我知道......很长的问题,但是我试图创建一个PageView简介我真的不甘示弱。任何和所有的帮助表示赞赏...

0 个答案:

没有答案