LINHONGJIE LINHONGJIE - 7 months ago 57
Swift Question

Value of type 'CASPageMenu' HAS NO MEMBER 'currentPageIndex'

I was a beginner, followed by open-source framework is doing, but this step is not to know where is the problem.

public class CAPSPageMenu: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate {
...
var currentPageIndex : Int = 0
...
}

var pageMenu : CAPSPageMenu?
func didTapGoToLeft() {
var currentIndex = pageMenu!.currentPageIndex # error

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



error message: Value of type 'CASPageMenu' HAS NO MEMBER 'currentPageIndex'


Thanks´╝ü

Answer

You must initialize pageMenu, otherwise it dont know how many pages it will have.

The line you have written it's not enough:

var pageMenu : CAPSPageMenu?

After you have write this:

var pageMenu : CAPSPageMenu?

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

inside viewDidAppear you can add this:

// MARK: - Scroll menu setup

// Initialize view controllers to display and place in array
var controllerArray : [UIViewController] = []
let controller1 : UIViewController! = UIViewController()
controller1.title = "TEST1"
controllerArray.append(controller1)
let controller2 : UIViewController! = UIViewController()
controller2.title = "TEST2"
controllerArray.append(controller2)
// Initialize scroll menu
// 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(40.0),
            .MenuItemWidth(90.0),
            .CenterMenuItems(true)
]
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 0.0, self.view.frame.width,self.view.frame.height), pageMenuOptions: parameters)
self.addChildViewController(pageMenu!)
self.view.addSubview(pageMenu!.view)
pageMenu!.didMoveToParentViewController(self)

Now when you didTapGoToLeft() pageMenu know exactly his current page, the total pages, the previous page and the next page where to going..