简体   繁体   中英

Swiping between View Controllers in swift

In Swift, I currently have the following code in my first view controller, yet when I swipe nothing happens? What is wrong with my code? Do I have to implement something else? Thanks alot SO users, appreciate it.

import UIKit


class ViewController: UIViewController, UIPageViewControllerDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


    }

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

    }

    var myViewControllers = Array(count: 4, repeatedValue:UIViewController())

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        let pvc = segue.destinationViewController as UIPageViewController

        pvc.dataSource = self

        let storyboard = UIStoryboard(name: "Main", bundle: nil);

        var vc0 = storyboard.instantiateViewControllerWithIdentifier("FirstViewController") as UIViewController
        var vc1 = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as UIViewController
        var vc2 = storyboard.instantiateViewControllerWithIdentifier("ThirdViewController") as UIViewController
        var vc3 = storyboard.instantiateViewControllerWithIdentifier("FourthViewController") as UIViewController

        self.myViewControllers = [vc0, vc1, vc2, vc3]

        pvc.setViewControllers([myViewControllers[1]], direction:.Forward, animated:false, completion:nil)

        println("Loaded")
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        var currentIndex =  find(self.myViewControllers, viewController)!+1
        if currentIndex >= self.myViewControllers.count {
            return nil
        }
        return self.myViewControllers[currentIndex]
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        var currentIndex =  find(self.myViewControllers, viewController)!-1
        if currentIndex < 0 {
            return nil
        }
        return self.myViewControllers[currentIndex]
    }

}

Sorry, your code is working perfectly for me. I made a trivial test (never having used a UIPageViewController before) with a button to segue from the initial VC to the Page VC, and two disconnected VCs in the storyboard to be constructed programatically with your code (I pasted it direct from above) and given to the Page VC. Each of the 3 visible pages was identified with a label. And they all work fine.

Does your UIPageViewController get instantiated? Do your two delegate routines above get called?

I suspect that you have not set up your storyboard appropriately. Here is mine:

故事板屏幕抓图

When iOS runs your app, it transfers control to the initial VC. That's the one that you're writing your code in, called ViewController in the above source. That is not a UIPageViewController ; it's an ordinary UIViewController , it says so here:

class ViewController: UIViewController, UIPageViewControllerDataSource {

The UIPageViewControllerDataSource bit just says it also provides the routines that tell a UIPageViewController what to display. When you do a segue out of ViewController , the prepareForSegue code runs then, and programs the destination of the segue, which is expected to be a UIPageViewController , with the other 4 VCs. To make the segue work, I dragged a UIButton to the first ViewController in storyboard, and dragged a UIPageViewController onto the right. Then control-drag from the button to the UIPageViewController. You should get a popup asking what kind of segue you want. I chose modal. When you run the app, you should see the button. When you press the button, you should see VC1, and swiping left or right will show the others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM