简体   繁体   English

快速在视图控制器之间滑动

[英]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? 在Swift中,当前我的第一个视图控制器中包含以下代码,但是当我滑动时什么都没有发生? What is wrong with my code? 我的代码有什么问题? Do I have to implement something else? 我还需要实施其他东西吗? Thanks alot SO users, appreciate it. 非常感谢SO用户,非常感谢。

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. 我做了一个简单的测试(以前从未使用过UIPageViewController),并使用一个按钮将其从初始VC筛选到Page VC,并将故事板上的两个断开连接的VC用您的代码以编程方式构造(我直接从上面粘贴了它),提供给Page VC。 Each of the 3 visible pages was identified with a label. 3个可见页面的每一个都用标签标识。 And they all work fine. 他们都工作正常。

Does your UIPageViewController get instantiated? 您的UIPageViewController是否实例化? 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. 当iOS运行您的应用程序时,它将控制权转移到初始VC。 That's the one that you're writing your code in, called ViewController in the above source. 这就是您在其中编写代码的代码,在上述源代码中称为ViewController That is not a UIPageViewController ; 那不是UIPageViewController ; it's an ordinary UIViewController , it says so here: 这是一个普通的UIViewController ,它在这里说:

class ViewController: UIViewController, UIPageViewControllerDataSource {

The UIPageViewControllerDataSource bit just says it also provides the routines that tell a UIPageViewController what to display. UIPageViewControllerDataSource位只是说它还提供了告诉UIPageViewController要显示什么的例程。 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. 当您从ViewController执行segue时,将运行prepareForSegue代码,并使用其他4个VC对segue的目的地(应该是UIPageViewController进行UIPageViewController To make the segue work, I dragged a UIButton to the first ViewController in storyboard, and dragged a UIPageViewController onto the right. 为了使segue正常工作,我将UIButton拖到情节UIPageViewController的第一个ViewController上,并将UIPageViewController拖到右侧。 Then control-drag from the button to the UIPageViewController. 然后,将控件从按钮拖动到UIPageViewController。 You should get a popup asking what kind of segue you want. 您应该弹出一个窗口,询问您想要哪种类型的segue。 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. 按下按钮时,您应该看到VC1,向左或向右滑动将显示其他按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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