问题
I am trying to make three images in a UIPageView
to open three different NavigationController
. Here is my layout as of right now. I connected three Custom Segue to each NavigationController
and under ContentViewController
, I added the following @IBAction
method:
@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)
}
And under the viewDidLoad
method:
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)
When I compile and run the app, it shows the three images in the PageView
but nothing is happening.
Any advice would be helpful!
Here is the rest of the code:
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
}
}
来源:https://stackoverflow.com/questions/32056307/using-uipageview-images-to-open-viewcontroller-w-navigationcontroller