Search code examples
iosuisegmentedcontrolswift4

'_.SegmentedControlItems' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector


I am programmatically creating a UISegmented control without storyboard.Instead of pulling segmented items from array, I am trying to use Model Class,

class SegmentedControlItems{
    let title: String

    init(title:String) {
        self.title = title
    }
}

Then, in table view function, I wrote the following code

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        headerView.backgroundColor = UIColor.white
        let items:[SegmentedControlItems] = {
            let item1 = SegmentedControlItems(title: "Repeat Task")
            let item2 = SegmentedControlItems(title: "One time task")
            return [item1, item2]
        }()

        let segmentedControl: UISegmentedControl = {

            let segmentedControl = UISegmentedControl(items: items)

            segmentedControl.tintColor = UIColor(red:0.44, green:0.75, blue:0.27, alpha:1.0)
            segmentedControl.selectedSegmentIndex = 0
            segmentedControl.translatesAutoresizingMaskIntoConstraints = false
            return segmentedControl
        }()
       headerView.addSubview(segmentedControl)

}

My app works fine if I pass the array of items. But it crashes after I wrote the code above. And gives me this error in console - SForwarding: warning: object 0x600000251640 of class 'Appname.SegmentedControlItems' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[Appname.SegmentedControlItems copy]

Please help


Solution

  • The issue here is that you are providing an array of objects (which in turn contains the values as an attribute) instead of passing an array of titles.

    If you want to use a model, you have to change the following line

    let segmentedControl = UISegmentedControl(items: items)
    

    to

    let segmentedControl: UISegmentedControl = UISegmentedControl(items: items.map({
            $0.title
        }))