Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Austin Murtha
Austin Murtha
4,843 Points

Unrecognized Selector Swift

I am doing a simple tutorial for segmented control, and I am getting unrecognized selector. I am missing something silly, just can't figure it out.

import UIKit

class ViewController: UIViewController {

override func loadView() {
    self.view.backgroundColor = UIColor.purpleColor()

    // Initialize segmented view
    let items = ["Purple", "Green", "Blue"]
    let customSC = UISegmentedControl(items: items)
    customSC.selectedSegmentIndex = 0

    // Set up the frame and segmented control
    let frame = UIScreen.mainScreen().bounds
    customSC.frame = CGRectMake(frame.minX + 10, frame.minY + 50, frame.width - 20, frame.height * 0.1)

    // Syling the controller ( not necessary)
    customSC.layer.cornerRadius = 5.0 // removes background bleed
    customSC.backgroundColor = UIColor.blackColor()
    customSC.tintColor = UIColor.whiteColor()

    // Add Target
    customSC.addTarget(self, action: "changeColor", forControlEvents: .ValueChanged)



func changeColor(sender: UISegmentedControl) {
    switch sender.selectedSegmentIndex {
    case 1:
        self.view.backgroundColor = UIColor.greenColor()
    case 2:
        self.view.backgroundColor = UIColor.blueColor()
        self.view.backgroundColor = UIColor.purpleColor()

override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.

override func didReceiveMemoryWarning() {
    // Dispose of any resources that can be recreated.


Austin Murtha
Austin Murtha
4,843 Points

This is the tutorial. I am just trying to get experience adding the view programatically instead of storyboards


1 Answer

Austin Murtha
Austin Murtha
4,843 Points

It was something stupid.... I didn't have a colon at the end of changeColor in the line

customSC.addTarget(self, action: "changeColor:", forControlEvents: .ValueChanged)