使用UIPageView图像打开带有NavigationController的ViewController

时间:2015-08-17 17:25:49

标签: ios swift uipageviewcontroller swrevealviewcontroller

我想在UIPageView中制作三张图片,以打开三张不同的NavigationController。这是right now的布局。我将三个自定义搜索连接到每个NavigationController,在ContentViewController下,我添加了以下@IBAction方法:

@IBAction func navJns(sender: AnyObject?){
        let storyboard = UIStoryboard(name: "Nav", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("Jeans") as! Jeans
        self.modalPresentationStyle = UIModalPresentationStyle.Custom

        presentViewController(controller, animated: true, completion: nil)
    }

    @IBAction func navBlusas(sender: AnyObject?){
        let storyboard = UIStoryboard(name: "Nav", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("Blusas") as! Blusas
        self.modalPresentationStyle = UIModalPresentationStyle.Custom

        presentViewController(controller, animated: true, completion: nil)
    }

    @IBAction func navLeggings(sender: AnyObject?){
        let storyboard = UIStoryboard(name: "Nav", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("Leggings") as! Leggings
        self.modalPresentationStyle = UIModalPresentationStyle.Custom

        presentViewController(controller, animated: true, completion: nil)
    }

viewDidLoad方法下:

var tapGesturePageView = UITapGestureRecognizer(target: self, action: "navJns:"); self.imageView.addGestureRecognizer(tapGesturePageView)

        var tapGesturePageView2 = UITapGestureRecognizer(target: self, action: "navBlusas:"); self.imageView.addGestureRecognizer(tapGesturePageView2)

        var tapGesturePageView3 = UITapGestureRecognizer(target: self, action: "navLeggings:"); self.imageView.addGestureRecognizer(tapGesturePageView3)

当我编译并运行应用程序时,它会显示PageView中的三个图像,但没有任何事情发生。

任何建议都会有所帮助!

以下是代码的其余部分:

ContentViewController.swift

import UIKit

class ContentViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!
    var pageIndex: Int!
    var imageFile: String!



    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.imageView.image = UIImage(named: self.imageFile)

        var tapGesturePageView = UITapGestureRecognizer(target: self, action: "navJns:"); self.imageView.addGestureRecognizer(tapGesturePageView)

        var tapGesturePageView2 = UITapGestureRecognizer(target: self, action: "navBlusas:"); self.imageView.addGestureRecognizer(tapGesturePageView2)

        var tapGesturePageView3 = UITapGestureRecognizer(target: self, action: "navLeggings:"); self.imageView.addGestureRecognizer(tapGesturePageView3)

    }

    @IBAction func navJns(sender: AnyObject?){
        let storyboard = UIStoryboard(name: "Nav", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("Jeans") as! Jeans
        self.modalPresentationStyle = UIModalPresentationStyle.Custom

        presentViewController(controller, animated: true, completion: nil)
    }

    @IBAction func navBlusas(sender: AnyObject?){
        let storyboard = UIStoryboard(name: "Nav", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("Blusas") as! Blusas
        self.modalPresentationStyle = UIModalPresentationStyle.Custom

        presentViewController(controller, animated: true, completion: nil)
    }

    @IBAction func navLeggings(sender: AnyObject?){
        let storyboard = UIStoryboard(name: "Nav", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("Leggings") as! Leggings
        self.modalPresentationStyle = UIModalPresentationStyle.Custom

        presentViewController(controller, animated: true, completion: nil)
    }

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



}

ViewController.swift

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource {


    var pageViewController: UIPageViewController!
    var pageImages: NSArray!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.pageImages = NSArray(objects: "jeans.png", "blusas.png", "leggings.png")

        self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
        self.pageViewController.dataSource = self

        var startVC = self.viewControllerAtIndex(0) as ContentViewController
        var viewControllers = NSArray(object: startVC)

        self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)

        self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height - 60)

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

    }

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

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

if index >= self.pageImages.count
        {
            return nil
        }

        var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController

        vc.imageFile = self.pageImages[index] as! String
        vc.pageIndex = index

        return vc


    }


    // MARK: - Page View Controller Data Source

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

        var vc = viewController as! ContentViewController
        var index = vc.pageIndex as Int


        if (index == 0) || (index == NSNotFound)
        {
            return nil

        }

        index--

        return self.viewControllerAtIndex(index)

    }

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

        var vc = viewController as! ContentViewController
        var index = vc.pageIndex as Int

        if (index == NSNotFound)
        {
            return nil
        }

        index++

        return self.viewControllerAtIndex(index)

    }

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


}

0 个答案:

没有答案