UIPageViewController在每个页面上都有按钮,它提供了一个详细的视图控制器

时间:2015-12-07 19:08:01

标签: ios swift uipageviewcontroller

我已经设置了一个集合视图,它在集合视图下方的容器中单击单元格时显示不同的视图控制器。这些不同的视图控制器由页面视图组成,这些页面视图呈现来自在集合视图中选择的相同类别的不同数量的产品。 我想在页面上添加一个按钮,以便每个页面都应该在内部触摸时显示有关该产品的详细信息(例如,弹出另一个视图控制器)。我现在无法想出任何东西,如果有人告诉我如何做到这一点,我将非常感激。 提前谢谢。

这是集合视图,单元格显示在容器中。

enter image description here

这是容器中显示的视图控制器。

enter image description here

以下是我的第一个产品页面视图的代码:

var pageViewController: UIPageViewController!
var pageTitles:NSArray!
var pageImages:NSArray!
var productTitle:NSArray!



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

    self.pageTitles = NSArray(objects: "За още по-ефективна подкрепа на вените.", "Живей с лекота без разширени вени.", "За ефективна подкрепа на вените.", "Единствените в България кърпички против хемороиди.")

    self.pageImages = NSArray(objects: "RuvenorForte.jpg", "RuvenorGel.jpg", "RuvenorKapsuli.jpg", "RuvenorKurpi.jpg", "ProbienFemiFlora.jpg")

    self.productTitle = NSArray(objects: "Рувенор Форте", "Рувенор гел", "Рувенор капсули","Рувенор кърпи")

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

    self.pageViewController.dataSource = self

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

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

    //self.pageViewController.view.frame = CGRectMake(0, 62, self.view.frame.width, self.view.frame.size.height - 60)
    let height = UIScreen.mainScreen().bounds.size.height
    if height == 568 {
        self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.size.height - 66)        }
    else if  height == 667{
        self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.size.height - 66)

    }
    else {
        self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.size.height - 230)        }

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


}


override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent    }

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


func viewControllerAtIndex(index: Int) -> ContentViewController
{
    if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
        return ContentViewController()
    }

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

    vc.imageFile = self.pageImages[index] as! String
    vc.titleText = self.pageTitles[index] as! String
    vc.product = self.productTitle[index] as! String
    vc.pageIndex = index


    return vc


}


// MARK: - Page View Controller Data Source

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

    let 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? {

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

    if (index == NSNotFound)
    {
        return nil
    }

    index++

    if (index == self.pageTitles.count)
    {
        return nil
    }

    return self.viewControllerAtIndex(index)

}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
    return self.pageTitles.count
}

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

这是来自视图控制器的代码,我传递了所有信息:

class ContentViewController:UIViewController {

@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var titleLabel: UILabel!

@IBOutlet var lightPurpleView: UIView!

@IBOutlet var productTitle: UILabel!

@IBOutlet var infoLabel: UILabel!

@IBOutlet var scrollView: UIScrollView!

@IBOutlet var infoTextField: UITextView!

@IBOutlet var infoTextField1: UITextView!

@IBOutlet var infoTextField2: UITextView!

var pageIndex: Int!
var titleText: String!
var imageFile: String!
var infoButton: UIButton!
var product: String!
var listovka: String!
var listovka1: String!
var listovka2: String!  



override func viewDidLoad()
{
    super.viewDidLoad()

    let upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))

    upSwipe.direction = .Up
    view.addGestureRecognizer(upSwipe)
    imageView.addGestureRecognizer(upSwipe)

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

    self.titleLabel.text = self.titleText

    self.infoTextField.text = self.listovka
    self.infoTextField1.text = self.listovka1
    self.infoTextField2.text = self.listovka2

    self.productTitle.text = self.product
    self.lightPurpleView.layer.cornerRadius = 10
    imageView.layer.cornerRadius = 10
    imageView.clipsToBounds = true
    self.infoLabel.text = "Листовка"
    scrollView.contentSize.height = 3375
    scrollView.layer.cornerRadius = 10
    self.infoTextField.layer.cornerRadius = 10
    self.infoTextField.clipsToBounds = true

    self.infoTextField1.layer.cornerRadius = 10
    self.infoTextField1.clipsToBounds = true
    self.infoTextField2.layer.cornerRadius = 10
    self.infoTextField2.clipsToBounds = true

    self.scrollView.showsVerticalScrollIndicator = false





    lightPurpleView.layer.shadowColor = UIColor.darkGrayColor().CGColor
    lightPurpleView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
    lightPurpleView.layer.shadowOpacity = 1.0
    lightPurpleView.layer.shadowRadius = 2
    lightPurpleView.layer.masksToBounds = true
    lightPurpleView.clipsToBounds = false

    imageView.layer.shadowColor = UIColor.darkGrayColor().CGColor
    imageView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
    imageView.layer.shadowOpacity = 1.0
    imageView.layer.shadowRadius = 2
    imageView.layer.masksToBounds = true
    imageView.clipsToBounds = false


}}

所以基本上我想添加这个按钮,它位于第二张图片上而不是向下滚动以查看textViews,只是将它们呈现在另一个视图控制器中(取决于该产品类别的页面视图的indexPath)弹出。

0 个答案:

没有答案