当我们按菜单水龙头时,CAPSPageMenu获取索引

时间:2019-06-12 09:31:04

标签: ios swift iphone xcode

我添加了CAPSPageMenu。当前索引始终为0。 我想知道我点击的菜单项的索引。

我尝试下面的代码,但是当我将断点添加到此委托方法时,未调用它。

我正在使用此lib:https://github.com/PageMenu/PageMenu/blob/master/README.md

class firstVC: UIViewController,CAPSPageMenuDelegate {

    var pageMenu : CAPSPageMenu?

    override func viewDidLoad() {
        super.viewDidLoad()
        pageMenu?.delegate=self
    }

    func willMoveToPage(_ controller: UIViewController, index: Int){   
        print(index)
    }

    func didMoveToPage(_ controller: UIViewController, index: Int){
        print(index)
    }

}

更新:

@IBOutlet var optionView: UIView!



    override func viewDidLoad() {
            super.viewDidLoad()


            var menuTitleFont: UIFont?
            if UIScreen.main.bounds.size.height == 568 {
                menuTitleFont = UIFont(name: "roboto-Bold", size: 10.0)
            }
            else {
                menuTitleFont = UIFont(name: "roboto-Bold", size: 12.0)
            }


            let parameters: [CAPSPageMenuOption] = [
                .scrollMenuBackgroundColor(UIColor.yellow),
                .viewBackgroundColor(UIColor.black),
                .selectionIndicatorColor(UIColor.white),
                .bottomMenuHairlineColor(UIColor.lightGray),
                .selectedMenuItemLabelColor(UIColor.white),
                .unselectedMenuItemLabelColor(UIColor.lightGray),
                //.menuItemFont(12),
                .menuHeight(50.0),
                .menuItemWidth(self.view.frame.size.width/10 + 30),
                .centerMenuItems(true)
            ]
            print(pageMenu)
            pageMenu!.delegate = self

            var controllerArray = [UIViewController]()

            if  let AllVc = self.storyboard?.instantiateViewController(withIdentifier:"AllVc") {
                AllVc.title = "AllVc"
                controllerArray.append(AllVc)
            }


            if  let baseVc = self.storyboard?.instantiateViewController(withIdentifier:"baseVc") {
                baseVc.title = "baseVc"
                controllerArray.append(baseVc)
            }




            pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x: 0.0, y:0.0, width:view.frame.width, height:self.view.frame.height), pageMenuOptions: parameters)



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

            pageMenu!.didMove(toParentViewController: self)
        }

这是我完整查看的已加载代码。

1 个答案:

答案 0 :(得分:0)

在初始化CAPSPageMenu之后,需要设置委托。

pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRect(x: 0.0, y:0.0, width:view.frame.width, height:self.view.frame.height), pageMenuOptions: parameters)
pageMenu!.delegate = self
print(pageMenu)