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! While you're at it, check out some resources Treehouse students have shared here.
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 trialAustin Murtha
4,843 PointsUnrecognized 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() {
super.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)
self.view.addSubview(customSC)
}
func changeColor(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 1:
self.view.backgroundColor = UIColor.greenColor()
case 2:
self.view.backgroundColor = UIColor.blueColor()
default:
self.view.backgroundColor = UIColor.purpleColor()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
1 Answer
Austin Murtha
4,843 PointsIt was something stupid.... I didn't have a colon at the end of changeColor in the line
customSC.addTarget(self, action: "changeColor:", forControlEvents: .ValueChanged)
Thanks
Austin Murtha
4,843 PointsAustin Murtha
4,843 PointsThis is the tutorial. I am just trying to get experience adding the view programatically instead of storyboards
http://www.richardhsu.me/posts/2015/01/26/segmented-control.html