Search code examples
swiftios8uipagecontroladdtarget

'unrecognized selector sent to instance' for UIPageControl ValueChanged event


I am trying to programmatically use addTarget() to add ValueChanged Event for UIPageControl object in mainView.

var pageControls = mainView.subviews.filter { $0 is UIPageControl } as [UIPageControl]
for p in pageControls
{
 println("\(p.tag)"     //p.tag printed as expected
 p.addTarget(self, action: "pageValueChanged:", forControlEvents: .ValueChanged)
}

and then

func pageValueChanged(sender: AnyObject) {
var pageControl = sender as UIPageControl
    println ("value changed")
}

There is 1 UIPageControl in mainview, I expect pageValueChanged will be trigger when click UIPageControl object to change its value. But when click the UIPageControl object, I get error "[Tmoji.KeyboardViewController pageValueChanged:]: unrecognized selector sent to instance".

I did the same for UIButton event TouchUpInside and it worked. Is there anything I missed?


Solution

  • a stupid mistake.

    It turned out that my function pageValueChanged was added within another function incorrectly, instead of being added in the class.