如何从可选的func didMoveToPage中获取索引?

时间:2015-10-28 09:14:13

标签: ios swift

我在我的应用程序中使用uacaps / CAPSPageMenu,我无法弄清楚如何获取活动页面的编号,以便我可以显示与该页面对应的网页。在代码CAPSPageMenu.swift中使用以下代码。

import UIKit

@objc public protocol CAPSPageMenuDelegate {
// MARK: - Delegate functions

optional func willMoveToPage(controller: UIViewController, index: Int)
optional func didMoveToPage(controller: UIViewController, index: Int)
}

我认为索引:可选函数didMoveToPage中的Int部分给了我一些当前页面。这是正确的吗?如何在ViewController.swift代码中获取该索引号?

修改
在CAPSPageMenu中,我找到了这段代码。

public func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    if scrollView.isEqual(controllerScrollView) {
        // Call didMoveToPage delegate function
        let currentController = controllerArray[currentPageIndex]
        delegate?.didMoveToPage?(currentController, index: currentPageIndex)

ViewController.swift代码如下所示。

//
//  ViewController.swift
//  PageMenuDemoStoryboard
//
//  Created by Niklas Fahl on 12/19/14.
//  Copyright (c) 2014 CAPS. All rights reserved.
//

import UIKit


var websiteArray = ["http://website_0", "http://website_1", "http://website_2"]


//class ViewController: UIViewController {
class ViewController: UIViewController,CAPSPageMenuDelegate {

var pageMenu : CAPSPageMenu?
var pageNumber = 0

@IBOutlet weak var showWebsite: UIWebView!

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)


    // MARK: - UI Setup
    self.title = "App Title"
    self.navigationController?.navigationBar.barTintColor = UIColor(red: 30.0/255.0, green: 30.0/255.0, blue: 30.0/255.0, alpha: 1.0)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
    self.navigationController?.navigationBar.barStyle = UIBarStyle.Black
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "<-", style: UIBarButtonItemStyle.Done, target: self, action: "didTapGoToLeft")
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "->", style: UIBarButtonItemStyle.Done, target: self, action: "didTapGoToRight")


    // MARK: - Scroll menu setup

    // Initialize view controllers to display and place in array
    var controllerArray : [UIViewController] = []

    let controller1 : TestCollectionViewController = TestCollectionViewController(nibName: "TestCollectionViewController", bundle: nil)
    controller1.title = "Item_0"
    controller1.photoNameArray = ["Item_0.png"]
    controllerArray.append(controller1)
    let controller2 : TestCollectionViewController = TestCollectionViewController(nibName: "TestCollectionViewController", bundle: nil)
    controller2.title = "Item_1"
    controller2.photoNameArray = ["Item_1.png"]
    controllerArray.append(controller2)
    let controller3 : TestCollectionViewController = TestCollectionViewController(nibName: "TestCollectionViewController", bundle: nil)
    controller3.title = "Item_2"
    controller3.photoNameArray = ["Item_2.png"]
    controllerArray.append(controller3)


    // Customize menu (Optional)
    let parameters: [CAPSPageMenuOption] = [
        .ScrollMenuBackgroundColor(UIColor(red: 30.0/255.0, green: 30.0/255.0, blue: 30.0/255.0, alpha: 1.0)),
        .ViewBackgroundColor(UIColor(red: 20.0/255.0, green: 20.0/255.0, blue: 20.0/255.0, alpha: 1.0)),
        .SelectionIndicatorColor(UIColor.orangeColor()),
        .BottomMenuHairlineColor(UIColor(red: 70.0/255.0, green: 70.0/255.0, blue: 80.0/255.0, alpha: 1.0)),
        .MenuItemFont(UIFont(name: "HelveticaNeue", size: 13.0)!),
        .MenuHeight(30.0),
        .MenuItemWidth(90.0),
        .CenterMenuItems(true)
    ]

    // Initialize scroll menu
    pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, self.view.frame.height * 0.71, self.view.frame.width, self.view.frame.height * 0.33), pageMenuOptions: parameters)

    self.addChildViewController(pageMenu!)
    self.view.addSubview(pageMenu!.view)

    pageMenu!.didMoveToParentViewController(self)

    pageMenu!.delegate = self


    func willMoveToPage(controller: UIViewController, index: Int) {
        let subview=controller as! ViewController
        subview.pageNumber=index;
    }

}


func didTapGoToLeft() {
    var currentIndex = pageMenu!.currentPageIndex

    if currentIndex > 0 {
        pageMenu!.moveToPage(currentIndex - 1)
    }
    setWebPage()
}


func didTapGoToRight() {
    var currentIndex = pageMenu!.currentPageIndex

    if currentIndex < pageMenu!.controllerArray.count {
        pageMenu!.moveToPage(currentIndex + 1)
    }
    setWebPage()
}


func setWebPage() {
    var currentIndex = pageMenu!.currentPageIndex
    switch currentIndex {
    case 0:
        let url = NSURL (string: "\(websiteArray[0])");
        let requestObj = NSURLRequest(URL: url!);
        showWebsite.loadRequest(requestObj);
        break

    case 1:
        let url = NSURL (string: "\(websiteArray[1])");
        let requestObj = NSURLRequest(URL: url!);
        showWebsite.loadRequest(requestObj);
        break

    case 2:
        let url = NSURL (string: "\(websiteArray[2])");
        let requestObj = NSURLRequest(URL: url!);
        showWebsite.loadRequest(requestObj);
        break

    default:
        break
    }
}


// MARK: - Container View Controller
override func shouldAutomaticallyForwardAppearanceMethods() -> Bool {
    return true
}

override func shouldAutomaticallyForwardRotationMethods() -> Bool {
    return true
}
}

当我点击按钮时,它就像它应该的那样工作。导航栏显示活动页面,显示属于活动页面的图片(collectionViewController),同时显示属于活动页面的网页。但我不想使用按钮。我想使用collectionViewController,这样当我将图像滑动到下一页时,将显示新图像(这已经可用)和相应的网页(由于未设置 pageMenu!.currentPageIndex,这不起作用)。
当我滑动图片时如何激活didTapGoToLeft和Right方法?

1 个答案:

答案 0 :(得分:2)

CAPSPageMenu提供了一个委托方法,通知其委托页面已更改。这些是您在问题中列出的委托方法。

因此,在ViewController类中,您需要实现这些委托方法。

首先,告诉编译器您的类实现了协议:

class ViewController:UIViewController,CAPSPageMenuDelegate

现在,您需要将视图控制器设置为委托。在创建CAPSPageMenu的地方,你需要这样的东西 -

pageMenu.delegate=self

最后,实现委托方法。 CAPSPageMenu提供了它管理的一组视图控制器。你没有给出这些的确切细节,但我假设他们实现了一些我称之为SubViewController的课程。在此类中定义整数属性pageNumber,然后您的委托方法可以简单地为 -

func willMoveToPage(controller: UIViewController, index: Int) {
    let subview=controller as! SubViewController
    subview.pageNumber=index;
}

然后,在您的SubViewController中,您可以在pageNumber上实现一个setter,它可以在页码更改时执行所需的操作。在您的委托方法中,您还可以使用索引来索引字符串数组(URL说),然后在子视图控制器上设置字符串/ URL属性 - 这取决于您要执行的操作。