有问题实现UIPageViewController

时间:2015-03-27 10:10:38

标签: ios swift

我尝试创建没有故事板的UIPageView,名为" IntroductionView"它的contentView名为" IntroductionContentView",它们都是UIViewController的子类。

问题是当我滑动来回更改页面时,它会显示指示器的错误视图和位置。

IntroductionView类:

class IntroductionView: UIViewController, UIPageViewControllerDataSource ,UIPageViewControllerDelegate  {

    var pageView : UIPageViewController?

    var viewControllers : NSArray?

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Here you can init your properties

    }


    required init(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {

        pageView = UIPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.Scroll, navigationOrientation: UIPageViewControllerNavigationOrientation.Horizontal, options: nil)

        pageView!.dataSource = self
        pageView!.delegate = self
        pageView!.view.frame = self.view.bounds

        var contentView : IntroductionContentView = self.viewControllerAtIndex(0)

        viewControllers  =  [contentView]

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

        self.view.addSubview(pageView!.view)

    }

    func viewControllerAtIndex(index: NSInteger) -> IntroductionContentView {

        var childViewController = IntroductionContentView(nibName: nil, bundle: nil)
        childViewController.index = index
        return childViewController

    }

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

        var contentView : IntroductionContentView =  viewController as IntroductionContentView

        contentView.index++

        if (contentView.index >= 5 ) {

            return nil
        }


        return self.viewControllerAtIndex(contentView.index)

    }

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


        var contentView : IntroductionContentView =  viewController as IntroductionContentView

        if (contentView.index == 0) {

            return nil;
        }

        if (contentView.index == 4) {

            contentView.index--
        }

        contentView.index--

        return self.viewControllerAtIndex(contentView.index)

    }

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

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

        return 0
    }

IntroductionContentView类:

class IntroductionContentView: UIViewController {

    var index : NSInteger = 0
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Here you can init your properties

    }


    required init(coder aDecoder: NSCoder) {
        print("init")
        super.init(coder: aDecoder)
    }


    override func viewDidLoad() {

        switch index
        {
        case 0:
            self.view.backgroundColor = UIColor.redColor()
        case 1:
            self.view.backgroundColor = UIColor.greenColor()
        case 2:
            self.view.backgroundColor = UIColor.blueColor()
        case 3:
            self.view.backgroundColor = UIColor.yellowColor()
        case 4:
            self.view.backgroundColor = UIColor.orangeColor()
        default:
            break
        }

        let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(0, 20, 100, 50)
        button.backgroundColor = UIColor.blackColor()
        button.setTitle("Test Button Page \(index)", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(button)

    }

要显示我调用的PageController:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    var introductionView = IntroductionView(nibName: nil, bundle: nil)
    window!.rootViewController = introductionView
    window!.makeKeyAndVisible()

    return true
}

谢谢。

1 个答案:

答案 0 :(得分:0)

Before和AfterViewController的逻辑中存在问题。请参阅下面的我的代码,它应该工作。干杯!

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    var contentView:PageViewController = viewController as! PageViewController
    var index = contentView.index as Int
    if (index == NSNotFound)
    {
        return nil
    }
    index++
    if (index == 5)
    {
        return nil
    }
    return self.viewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    var contentView:PageViewController = viewController as! PageViewController
    var index = contentView.index as Int
    if (index == 0 || index == NSNotFound)
    {
        return nil
    }
    index--
    return self.viewControllerAtIndex(index)
}