-1

I'd like to implement a segmented control in Cocoa, not iOS. I created and populated a Segmented Control in Xcode's IDE and connected it to an IBOutlet:

@IBOutlet weak var prepChoice: NSSegmentedControl!

I then attached it to this function:

@IBAction func getprepChoice(_ sender: Any) {

    switch prepChoice.indexOfSelectedItem
    {
    case 0:
        print( "zero" )
    case 1:
        print( "one" )
    default:
        break
    }
}

But I am getting errors along the lines of "[General] -[NSView indexOfSelectedItem]: unrecognized selector sent to instance"

What does this error mean?

Dribbler
  • 4,343
  • 10
  • 33
  • 53
  • 2
    It appears you have connected a plain `NSView` to your `getprepChoice` method. You need to connect the actual segmented control. – rmaddy Mar 30 '18 at 23:26
  • Thanks super, @rmaddy --that was exactly it. Not having used this before, I assumed it was that I was using the function incorrectly. Silly mistake. – Dribbler Mar 30 '18 at 23:31
  • Possible duplicate of [How to draw segmented control with blue tint like Xcode?](https://stackoverflow.com/questions/24927874/how-to-draw-segmented-control-with-blue-tint-like-xcode) – l'L'l Mar 31 '18 at 05:43

1 Answers1

0

Under normal circumstances when making such an idiotic mistake as not connecting in Xcode the proper container, I would just delete this question. However, the error was so specific and @rmaddy diagnosed it right away, that I'm leaving it up for others who might be searching for the "unrecognized selector sent to instance" error in this context. The moral of the story is that Xcode does make things really easy by dragging from the xib GUI to the Swift code to connect GUI widgets to their coding implementations, but it's also easy to accidentally hook up the wrong container. If you get this error, go back to your xib GUI and make sure that the right container is selected when you create your instance variable.

Dribbler
  • 4,343
  • 10
  • 33
  • 53